如何将三个subprocess.popen组合成一行?

时间:2015-04-13 19:17:43

标签: python

我目前正在使用

p1 = subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', 'bash'], stdin=p1.stdout,    stdout=subprocess.PIPE)
p3 = subprocess.Popen(['wc', '-l'], stdin=p2.stdout, stdout=subprocess.PIPE)
count = int(p3.stdout.read())

if count == 2:
   print count
   print "yes"
else:
   print "fail"

检查我的python当前是否已经运行。 它工作正常。

但是,我想知道我是否可以将p1 p2p3合并为一行。

我试过

p = subprocess.Popen(['ps ax | grep bash | wc -l'], stdout=subprocess.PIPE) 

还有更多,但它不起作用。有没有办法将所有这3个组合成一行?

2 个答案:

答案 0 :(得分:1)

如果将|关键字参数传递给shell=True,则只能使用shell样式的管道(Popen):

p = subprocess.Popen('ps ax | grep bash | wc -l', stdout=subprocess.PIPE, shell=True)

否则,您传递的整个字符串将被视为可执行文件的名称,这将失败,因为您没有名为ps ax | grep bash | wc -l的程序。

此外,您不应该将命令作为带有shell=True的列表传递,您只需传递一个字符串。

另一个注意事项:您可能需要调整命令才能使其正常工作,因此grep bash命令本身不会计入ps ax | grep bash的输出中。这样做的一个技巧是使用ps ax | grep [b]ash。此外,使用shell=True可能最终会启动bash实例来运行命令,因此您可能需要使用if count == 3而不是if count == 2

完全放弃:

p = subprocess.Popen('ps ax | grep [b]ash | wc -l', stdout=subprocess.PIPE, shell=True)

count = int(p.stdout.read())

if count == 2: # Or maybe 3?
   print count
   print "yes"
else:
   print "fail"

修改

这是在我的机器上运行不同版本代码的输出

>>> p1 = subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(['grep', 'bash'], stdin=p1.stdout,    stdout=subprocess.PIPE)
>>> p3 = subprocess.Popen(['wc', '-l'], stdin=p2.stdout, stdout=subprocess.PIPE); p3.stdout.read()
'42\n'
>>> p1 = subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(['grep', 'bash'], stdin=p1.stdout,    stdout=subprocess.PIPE)
>>> p3 = subprocess.Popen(['wc', '-l'], stdin=p2.stdout, stdout=subprocess.PIPE); p3.stdout.read()
'42\n'
>>> p1 = subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(['grep', 'bash'], stdin=p1.stdout,    stdout=subprocess.PIPE)
>>> p3 = subprocess.Popen(['wc', '-l'], stdin=p2.stdout, stdout=subprocess.PIPE); p3.stdout.read()
'42\n'
>>> p1 = subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(['grep', 'bash'], stdin=p1.stdout,    stdout=subprocess.PIPE)
>>> p3 = subprocess.Popen(['wc', '-l'], stdin=p2.stdout, stdout=subprocess.PIPE); p3.stdout.read()
'42\n'
>>> p1 = subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(['grep', 'bash'], stdin=p1.stdout,    stdout=subprocess.PIPE)
>>> p3 = subprocess.Popen(['wc', '-l'], stdin=p2.stdout, stdout=subprocess.PIPE); p3.stdout.read()
'42\n'
>>> p = subprocess.Popen('ps -ef | grep bash | wc -l ', stdout=subprocess.PIPE, shell=True) ; print(''.join(p.stdout.readlines()))
44

>>> p = subprocess.Popen('ps -ef | grep bash | wc -l ', stdout=subprocess.PIPE, shell=True) ; print(''.join(p.stdout.readlines()))
44

>>> p = subprocess.Popen('ps -ef | grep bash | wc -l ', stdout=subprocess.PIPE, shell=True) ; print(''.join(p.stdout.readlines()))
44

>>> p = subprocess.Popen('ps -ef | grep [b]ash | wc -l ', stdout=subprocess.PIPE, shell=True) ; print(''.join(p.stdout.readlines()))
42

>>> p = subprocess.Popen('ps -ef | grep [b]ash | wc -l ', stdout=subprocess.PIPE, shell=True) ; print(''.join(p.stdout.readlines()))
42

>>> p = subprocess.Popen('ps -ef | grep [b]ash | wc -l ', stdout=subprocess.PIPE, shell=True) ; print(''.join(p.stdout.readlines()))
42

答案 1 :(得分:0)

这是一个明显的答案。我所做的就是用第一个作业替换第二个p1和p2。

p3 = subprocess.Popen(['wc', '-l'], stdin=subprocess.Popen(['grep', 'bash'], \
     stdin=subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE).stdout, \
     stdout=subprocess.PIPE).stdout, stdout=subprocess.PIPE)