0

I'm maintaining a Perforce server via a Unix executable which logs the errors in the terminal whenever something happens to it. What I would like to do is use Python to start the executable, and then have it watch for any errors being logged in the terminal window from the application it's executing.

The following code works for execution, but I have no idea how to get data back from the subprocess when it gets logged to the terminal via some form of callback.

import subprocess subprocess.call(["./p4d"]) 

2 Answers 2

1

I usually do it this way

from subprocess import Popen, PIPE proc = Popen(["./p4d"], stdout=PIPE, stderr=PIPE) out, err = pipe.communicate() 

pipe.communicate() will hang until the process is finished and then returns stdout and stderr

Sign up to request clarification or add additional context in comments.

3 Comments

Will this log errors as they occur, or just when the process exits?
You get all of the logs but only once the process ends. Reading stdout/stderr while the process is running is a little bit trickier.
0

process.stdout and process.stdout are file-like objects that you can read from. Assuming things are logged to stdout:

proc = Popen(['./p4d'], stdout=PIPE, encoding='utf-8') # Or whatever encoding suits for line in proc.stdout: # Note that line will end in `\n`, unless it is the last line. line = line.rstrip('\n') # Do something depending on the line 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.