How to use `subprocess` command with pipes

后端 未结 9 1447
春和景丽
春和景丽 2020-11-22 02:32

I want to use subprocess.check_output() with ps -A | grep \'process_name\'. I tried various solutions but so far nothing worked. Can someone guide

9条回答
  •  梦如初夏
    2020-11-22 03:14

    JKALAVIS solution is good, however I would add an improvement to use shlex instead of SHELL=TRUE. below im grepping out Query times

    #!/bin/python
    import subprocess
    import shlex
    
    cmd = "dig @8.8.4.4 +notcp www.google.com|grep 'Query'"
    ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    output = ps.communicate()[0]
    print(output)
    

提交回复
热议问题