Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start detached infinite process with python on windows and pipe the output into a file

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.

like image 850
David Lichterov Avatar asked May 12 '26 19:05

David Lichterov


1 Answers

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.

like image 75
RoadRunner Avatar answered May 15 '26 07:05

RoadRunner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!