質問

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 awk sleep.sh '.execute();

Process all = proc1 | proc2 | proc3 | proc4;

// I tried this too and this didnt work

//println( [ 'sh', '-c', 'ps -ef | grep "sleep.sh" | grep -v "grep" |     awk "sleep.groovy" ' ].execute().text )

//also tried without the awk

println all.text;

Okay so what I am trying to do is ps the shell script i made (sleep.sh) [all it does it sleep for a period of time]. Not quite sure how to do that. This was my best guess^^

result:

-sh-3.2$ ./callGroovy.sh testSleep.groovy


-sh-3.2$

doesnt print anything out and doesnt give me anything (callGroovy is a shell script i use to call my groovy script) If i run the piped commands they work still except the awk I think i am doing the awk wrong heres the rest piped

-sh-3.2$ ps -ef | grep "sleep.sh" | grep -v "grep"
wasadmin ***** *****  0 **:** pts/1    **:**:** /bin/bash ./sleep.sh  

(where all the * are numbers)

when i try the script with just the grep and ps it doesnt give me this output either. any suggestions? ..PS Also I tried with and without the quotes in the groovy script. Didnt think it would make a difference but worth a shot

役に立ちましたか?

解決

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top