Groovy process not working with linux shell (grep and awk and ps)

后端 未结 1 2005
悲&欢浪女
悲&欢浪女 2021-01-27 18:27
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         


        
相关标签:
1条回答
  • 2021-01-27 18:54

    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
    
    0 讨论(0)
提交回复
热议问题