How to use `subprocess` command with pipes

后端 未结 9 1445
春和景丽
春和景丽 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 02:52

    Or you can always use the communicate method on the subprocess objects.

    cmd = "ps -A|grep 'process_name'"
    ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    output = ps.communicate()[0]
    print(output)
    

    The communicate method returns a tuple of the standard output and the standard error.

提交回复
热议问题