Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start daemon process from python on windows?

Can my python script spawn a process that will run indefinitely?

I'm not too familiar with python, nor with spawning deamons, so I cam up with this:

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NEW_CONSOLE
subprocess.Popen(executable, close_fds = True, startupinfo = si)

The process continues to run past python.exe, but is closed as soon as I close the cmd window.

like image 955
zzandy Avatar asked Oct 11 '12 16:10

zzandy


People also ask

How do I run a daemon process in python?

Daemon processes in Python To execute the process in the background, we need to set the daemonic flag to true. The daemon process will continue to run as long as the main process is executing and it will terminate after finishing its execution or when the main program would be killed.

What is the equivalent of daemon process in Windows?

In Microsoft Windows, software that runs as a non-interactive background process is called a service. A Windows service performs roughly the same role as a Linux or Unix daemon.


1 Answers

Using the answer Janne Karila pointed out this is how you can run a process that doen't die when its parent dies, no need to use the win32process module.

DETACHED_PROCESS = 8
subprocess.Popen(executable, creationflags=DETACHED_PROCESS, close_fds=True)

DETACHED_PROCESS is a Process Creation Flag that is passed to the underlying CreateProcess function.

like image 144
zzandy Avatar answered Sep 22 '22 11:09

zzandy