Linux 如何将`subprocess`命令与管道一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13332268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use `subprocess` command with pipes
提问by zuberuber
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 me how to do it?
我想subprocess.check_output()与ps -A | grep 'process_name'. 我尝试了各种解决方案,但到目前为止没有任何效果。有人可以指导我怎么做吗?
采纳答案by Taymon
To use a pipe with the subprocessmodule, you have to pass shell=True.
要在subprocess模块中使用管道,您必须通过shell=True.
However, this isn't really advisable for various reasons, not least of which is security. Instead, create the psand grepprocesses separately, and pipe the output from one into the other, like so:
但是,出于各种原因,这并不是真正可取的,其中最重要的是安全性。相反,分别创建ps和grep进程,并将输出从一个管道传输到另一个,如下所示:
ps = subprocess.Popen(('ps', '-A'), stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', 'process_name'), stdin=ps.stdout)
ps.wait()
In your particular case, however, the simple solution is to call subprocess.check_output(('ps', '-A'))and then str.findon the output.
但是,在您的特定情况下,简单的解决方案是调用subprocess.check_output(('ps', '-A'))然后str.find输出。
回答by jkalivas
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.
通信方法返回标准输出和标准错误的元组。
回答by amoffat
回答by Shooe
Also, try to use 'pgrep'command instead of 'ps -A | grep 'process_name'
另外,尝试使用'pgrep'命令而不是'ps -A | grep 'process_name'
回答by AlcubierreDrive
See the documentation on setting up a pipeline using subprocess: http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline
请参阅有关使用子进程设置管道的文档:http: //docs.python.org/2/library/subprocess.html#replacing-shell-pipeline
I haven't tested the following code example but it should be roughly what you want:
我还没有测试以下代码示例,但它应该是您想要的大致内容:
query = "process_name"
ps_process = Popen(["ps", "-A"], stdout=PIPE)
grep_process = Popen(["grep", query], stdin=ps_process.stdout, stdout=PIPE)
ps_process.stdout.close() # Allow ps_process to receive a SIGPIPE if grep_process exits.
output = grep_process.communicate()[0]
回答by Daniel Smith
JKALAVIS solution is good, however I would add an improvement to use shlex instead of SHELL=TRUE. below im grepping out Query times
JKALAVIS 解决方案很好,但是我会添加改进以使用 shlex 而不是 SHELL=TRUE。下面我正在寻找查询时间
#!/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)
回答by zingi
After Python 3.5you can also use:
在Python 3.5 之后,您还可以使用:
import subprocess
f = open('test.txt', 'w')
process = subprocess.run(['ls', '-la'], stdout=subprocess.PIPE, universal_newlines=True)
f.write(process.stdout)
f.close()
The execution of the command is blocking and the output will be in process.stdout.
命令的执行被阻塞,输出将在process.stdout 中。

