Groovy进程不能使用linux shell(grep和awk和ps) [英] Groovy process not working with linux shell (grep and awk and ps)

查看:506
本文介绍了Groovy进程不能使用linux shell(grep和awk和ps)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 进程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();

全部处理= proc1 | proc2 | proc3 | proc4;

//我也试过了,这个没有效果

// println(['sh','-c','ps -ef | grep'sleep.sh | grep -vgrep| awksleep.groovy'] .execute()。text)

//也尝试不使用awk

println all。文本;

好的,我想要做的是ps我制作的shell脚本(sleep.sh)[它只是睡了一段时间]。不太确定该怎么做。这是我最好的猜测^^



结果:

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

$ b -sh-3.2 $

不打印任何东西,并且不给我任何东西(callGroovy是我用来调用我的groovy脚本的shell脚本)
如果我运行管道命令,它们仍然工作,除了awk
我认为我在做awk错误
heres其余的管道。

$ p $ -sh-3.2 $ ps -ef | grepsleep.sh| grep -vgrep
wasadmin ***** ***** 0 **:** pts / 1 **:**:** / bin / bash ./sleep.sh

(其中所有*都是数字)



我试着用grep和ps脚本,它也不给我这个输出。有什么建议么? ..PS我也尝试过和没有groovy脚本中的引号。没有想到这会有所作为,但值得一试

$ c>选项只需要一个参数。试试这个命令行,你会发现它也失败了:

  sh -c ps -ef | sh -c grep sleep.sh | sh -c grep -v grep | sh -c awk sleep.sh 

它需要引号才能正常工作:

  sh -cps -ef| sh -cgrep sleep.sh| sh -cgrep -v grep| sh -cawk sleep.sh

您可以通过从列表字符串而不是字符串: proc1 = ['sh','-c','ps -ef'] 。在这种情况下,你需要在groovy中进行过滤,所以简单的解决方案就是不通过shell调用这些命令。试试这个:

 进程proc1 ='ps -ef'.execute()
进程proc2 ='grep sleep。 sh'.execute()
处理proc3 ='grep -v grep'.execute()
处理proc4 ='awk sleep.sh'.execute()

处理所有= proc1 | proc2 | proc3 | proc4

println all.text

最后,如果不起作用正确地说,使用

  println all.err.text 
阅读stderr流会很有帮助$ c>


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

这篇关于Groovy进程不能使用linux shell(grep和awk和ps)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆