torsdag 3 januari 2013

External processes via groovy script

Had one problem one trying to kick off an external process from a groovy script. Actually two problems:

1. Not getting desired output from process (java process with INFO logging)
Solution:
process.consumeProcessOutput(System.out, System.err)

Add that method to your process that you want to kick off and it will print the stuff you want.

2. Could not build up my argument list correctly
Well I had laborated with a linux command like """ping -c 2 localhost""" and it worked like a charm to just add """my command""".execute(). However when I started to get the arguments passed from another script they got separated.
My solution was like this:
Build up the strings again and then use ProcessBuilder. Like this:

def process = new ProcessBuilder(command).redirectErrorStream(true).start();

Where the command is a ArrayList with combined parameters. Like:

def command = []
command[0] = "ping"
command[1] = "-c"
command[2] = "2"
command[3] = "localhost"

Could probably be used nice but for this time it worked nice.

Inga kommentarer:

Skicka en kommentar