Vincent Mao Vincent Mao - 10 months ago 50
Bash Question

python how to use subprocess pipe with linux shell

I have a python script search for logs, it continuously output the logs found and I want to use linux pipe to filter the desired output. example like that:

$python | grep timeout

The problem is the sort and wc are blocked until the finishes, while the will continuous output the result.


p = subprocess.Popen("ping", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for line in p.stdout:


figured out, just change the stdout in subprocess to sys.stdout, python will handle the pipe for you.

p = subprocess.Popen("ping -c 5", shell=True, stdout=**sys.stdout**)

Thanks for all of you help!

Answer Source

And why use grep? Why don't do all the stuff in Python?

from subprocess import Popen, PIPE
p = Popen("ping", shell=True, stdin=PIPE, stdout=PIPE)
for line in p.stdout:
    if 'timeout' in line.split():
        # Process the error
        print("Timeout error!!")