1

I have a python script (test.py) using subprocess to execute some bash commands and write bash commands stdout/stderr to a log file. It's working fine if running python in foreground like this:

python test.py -i 123 -d 567

However, if running test.py in background like this, subprocess seems doesn't send stdout/stderr to the log file any more.

python test.py -i 123 -d 567 > /dev/null 2>&1 & disown

Here is the code snippet about subprocess:

process = subprocess.Popen(["xxx","xxx"], stdout=PIPE, stderr=PIPE)
stdoutdata, stderrdata = process.communicate()
log_output(stdoutdata, stderrdata)

where log_output() is a method using logging to write message to a file.

Please advise how to run python in background meanwhile send stdout/stderr to a log file.

Thanks!

3
  • I might be missing something, but it's a little hard to say without knowing what your "xxx" is doing.
    – Iguananaut
    Nov 7, 2018 at 21:59
  • You may be missing an error message by piping stderr and stdout (on the command-line) when you're trying to run it in the background.
    – martineau
    Nov 7, 2018 at 22:48
  • The xxx is the beeline command used to execute hive sql. For example, Popen(["beeline", "-u", connection, "-f", hive_file], stdout=PIPE, stderr=PIPE). It's working fine in foreground. Thanks in advance. Nov 7, 2018 at 22:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.