How to start daemon process from python on windows? How to start daemon process from python on windows? windows windows

How to start daemon process from python on windows?


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 = 8subprocess.Popen(executable, creationflags=DETACHED_PROCESS, close_fds=True)

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


This question was asked 3 years ago, and though the fundamental details of the answer haven't changed, given its prevalence in "Windows Python daemon" searches, I thought it might be helpful to add some discussion for the benefit of future Google arrivees.

There are really two parts to the question:

  1. Can a Python script spawn an independent process that will run indefinitely?
  2. Can a Python script act like a Unix daemon on a Windows system?

The answer to the first is an unambiguous yes; as already pointed out; using subprocess.Popen with the creationflags=subprocess.CREATE_NEW_PROCESS_GROUP keyword will suffice:

import subprocessindependent_process = subprocess.Popen(    'python /path/to/file.py',    creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)

Note that, at least in my experience, CREATE_NEW_CONSOLE is not necessary here.

That being said, the behavior of this strategy isn't quite the same as what you'd expect from a Unix daemon. What constitutes a well-behaved Unix daemon is better explained elsewhere, but to summarize:

  1. Close open file descriptors (typically all of them, but some applications may need to protect some descriptors from closure)
  2. Change the working directory for the process to a suitable location to prevent "Directory Busy" errors
  3. Change the file access creation mask (os.umask in the Python world)
  4. Move the application into the background and make it dissociate itself from the initiating process
  5. Completely divorce from the terminal, including redirecting STDIN, STDOUT, and STDERR to different streams (often DEVNULL), and prevent reacquisition of a controlling terminal
  6. Handle signals, in particular, SIGTERM.

The reality of the situation is that Windows, as an operating system, really doesn't support the notion of a daemon: applications that start from a terminal (or in any other interactive context, including launching from Explorer, etc) will continue to run with a visible window, unless the controlling application (in this example, Python) has included a windowless GUI. Furthermore, Windows signal handling is woefully inadequate, and attempts to send signals to an independent Python process (as opposed to a subprocess, which would not survive terminal closure) will almost always result in the immediate exit of that Python process without any cleanup (no finally:, no atexit, no __del__, etc).

Rolling your application into a Windows service, though a viable alternative in many cases, also doesn't quite fit. The same is true of using pythonw.exe (a windowless version of Python that ships with all recent Windows Python binaries). In particular, they fail to improve the situation for signal handling, and they cannot easily launch an application from a terminal and interact with it during startup (for example, to deliver dynamic startup arguments to your script, say, perhaps, a password, file path, etc), before "daemonizing". Additionally, Windows services require installation, which -- though perfectly possible to do quickly at runtime when you first call up your "daemon" -- modifies the user's system (registry, etc), which would be highly unexpected if you're coming from a Unix world.

In light of that, I would argue that launching a pythonw.exe subprocess using subprocess.CREATE_NEW_PROCESS_GROUP is probably the closest Windows equivalent for a Python process to emulate a traditional Unix daemon. However, that still leaves you with the added challenge of signal handling and startup communications (not to mention making your code platform-dependent, which is always frustrating).

That all being said, for anyone encountering this problem in the future, I've rolled a library called daemoniker that wraps both proper Unix daemonization and the above strategy. It also implements signal handling (for both Unix and Windows systems), and allows you to pass objects to the "daemon" process using pickle. Best of all, it has a cross-platform API:

from daemoniker import Daemonizerwith Daemonizer() as (is_setup, daemonizer):    if is_setup:        # This code is run before daemonization.        do_things_here()    # We need to explicitly pass resources to the daemon; other variables    # may not be correct    is_parent, my_arg1, my_arg2 = daemonizer(        path_to_pid_file,        my_arg1,        my_arg2    )    if is_parent:        # Run code in the parent after daemonization        parent_only_code()# We are now daemonized, and the parent just exited.code_continues_here()


For that purpose you could daemonize your python process or as you are using windows environment you would like to run this as a windows service.

You know i like to hate posting only web-links:

But for more information according to your requirement:

A simple way to implement Windows Service. read all comments it will resolve any doubt

If you really want to learn more

First read this

what is daemon process or creating-a-daemon-the-python-way

update:Subprocess is not the right way to achieve this kind of thing