Starting a child process with stdout / stderr redirected to a file

I have a Python script that I want to create a child process with stdout and stderr redirected to a file. My current approach is as follows:

import subprocess

# Do some stuff.
p = subprocess.Popen("/path/to/something",
                     stdout=open("/var/log/something.log", "a+"),
                     stderr=subprocess.STDOUT)
# Do some more stuff.
# Terminate the parent process and leave the child running and logging output.

This is similar to what I want, but nothing is written to the log file from stdout until the child process is interrupted. Without changing the child process, is it possible to release the output stream in the same way as if stdout were displayed in the console? Stderr seems to work fine.

I tried using bufsize=1as in the docs say: "1 means line buffering."

+4
source share

Source: https://habr.com/ru/post/1547207/


All Articles