0

How would I do the following subprocess command in python?

$ ps aux|grep python

>>> subprocess.check_output(['ps', 'aux', 'grep', 'python']) ?
David542
  • 104,438
  • 178
  • 489
  • 842
  • You need to create two subprocesses, one for `ps aux`, the other for `grep python`. Connect the output pipe of the first to the input pipe of the second. – Barmar Dec 22 '16 at 00:19
  • 2
    see http://stackoverflow.com/questions/6780035/python-how-to-run-ps-cax-grep-something-in-python – Paul Rooney Dec 22 '16 at 00:19

1 Answers1

5

You can do the following:

ps = subprocess.Popen(('ps', 'aux'), stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', 'python'), stdin=ps.stdout)
ps.wait()

print output
David542
  • 104,438
  • 178
  • 489
  • 842