Process proc1 =\'sh -c ps -ef\'.execute();
Process proc2 =\'sh -c grep sleep.sh \'.execute();
Process proc3 =\'sh -c grep -v grep \'.execute();
Process proc4 =\'sh -c aw
The shell -c
option expects one parameter only. Try this from the command line, and you'll see it fails as well:
sh -c ps -ef | sh -c grep sleep.sh | sh -c grep -v grep | sh -c awk sleep.sh
It needs quotes to work properly:
sh -c "ps -ef" | sh -c "grep sleep.sh" | sh -c "grep -v grep" | sh -c "awk sleep.sh"
You can quote the commands properly by starting with a list of strings instead of a string: proc1 = ['sh', '-c', 'ps -ef']
. In this case you're doing the filtering in groovy, so the simple solution is to simply not invoke the commands through the shell. Try this:
Process proc1 ='ps -ef'.execute()
Process proc2 ='grep sleep.sh '.execute()
Process proc3 ='grep -v grep '.execute()
Process proc4 ='awk sleep.sh '.execute()
Process all = proc1 | proc2 | proc3 | proc4
println all.text
Finally, if things don't work properly, it can be helpful to read the stderr stream with
println all.err.text