1

I have a python program which executes subprocess.Popen, like this;

process = subprocess.Popen(stand_alone_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
print "out: ", out
print "err: ", err

If my stand_alone_command will run forever, how do I get whatever stand_alone_command is throwing at STDOUT and STDERR so that I can log it.

Cœur
  • 34,719
  • 24
  • 185
  • 251
Nagri
  • 2,759
  • 4
  • 29
  • 58
  • Possible duplicate of [Run command and get its stdout, stderr separately in near real time like in a terminal](https://stackoverflow.com/questions/31926470/run-command-and-get-its-stdout-stderr-separately-in-near-real-time-like-in-a-te) – tripleee Nov 06 '18 at 14:08

1 Answers1

-1

Try reading from stdout instead of calling communicate() such as..

import subprocess
sac = ['tail', '-f', '/var/log/syslog']
process = subprocess.Popen(sac, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while 1:
    line = process.stdout.readline()
    if line:
        print(line)

I think you'll need to set shell=False but I'm on Linux and Windows is a bit different.

bivouac0
  • 2,239
  • 1
  • 12
  • 26