I have a Python script which I want to spawn a child process with stdout and stderr redirected to a file. My current approach looks like this:
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 appears to do what I want, but nothing is logged to the log file from stdout until the child process is interrupted. Without modifying the child process, is it possible to get the output to be flushed in a similar fashion as it is when stdout is displayed in the console? Stderr appears to flush normally.
I've tried using bufsize=1
as the docs say "1 means line buffered".