I am trying to start a process with python that runs infinitely and pipe it's output into a file. The new created process should keep running after the python executable exits. I am able to start a process and keep it running when python exits with this code:
subprocess.Popen(command_list, creationflags=subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_BREAKAWAY_FROM_JOB)
Now I am trying to pipe the output of the started process into a file, but i can't get it working together with detaching the process.
Any suggestions on how to achieve this ?
please note that i am trying to achieve this on Windows.
Have you tried just redirecting stderr and stdout to two files:
from subprocess import Popen
from subprocess import DEVNULL
from subprocess import DETACHED_PROCESS
from subprocess import CREATE_NEW_PROCESS_GROUP
from subprocess import CREATE_BREAKAWAY_FROM_JOB
command_list = ...
creationflags = DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP | CREATE_BREAKAWAY_FROM_JOB
with open("stdout.txt", mode="wb") as out, open("stderr.txt", mode="wb") as err:
Popen(command_list, creationflags=creationflags, stdin=DEVNULL, stdout=out, stderr=err)
Make sure to check stderr.txt and stdout.txt, since the output from your process could be redirected to either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With