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
  • 37,241
  • 25
  • 195
  • 267
Nagri
  • 3,008
  • 5
  • 34
  • 63
  • 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,494
  • 1
  • 13
  • 28