Searching on Google reveals x2 code snippets. The first result is to this code recipe which has a lot of documentation and explanation, along with some useful discussion underneath.
However, another code sample, whilst not containing so much documentation, includes sample code for passing commands such as start, stop and restart. It also creates a PID file which can be handy for checking if the daemon is already running etc.
These samples both explain how to create the daemon. Are there any additional things that need to be considered? Is one sample better than the other, and why?
This function will transform an application to a daemon:
There are many fiddly things to take care of when becoming a well-behaved daemon process:
prevent core dumps (many daemons run as root, and core dumps can contain sensitive information)
behave correctly inside a
chroot
gaolset UID, GID, working directory, umask, and other process parameters appropriately for the use case
relinquish elevated
suid
,sgid
privilegesclose all open file descriptors, with exclusions depending on the use case
behave correctly if started inside an already-detached context, such as
init
,inetd
, etc.set up signal handlers for sensible daemon behaviour, but also with specific handlers determined by the use case
redirect the standard streams
stdin
,stdout
,stderr
since a daemon process no longer has a controlling terminalhandle a PID file as a cooperative advisory lock, which is a whole can of worms in itself with many contradictory but valid ways to behave
allow proper cleanup when the process is terminated
actually become a daemon process without leading to zombies
Some of these are standard, as described in canonical Unix literature (Advanced Programming in the UNIX Environment, by the late W. Richard Stevens, Addison-Wesley, 1992). Others, such as stream redirection and PID file handling, are conventional behaviour most daemon users would expect but that are less standardised.
All of these are covered by the PEP 3143 “Standard daemon process library” specification. The python-daemon reference implementation works on Python 2.7 or later, and Python 3.2 or later.
Here's my basic 'Howdy World' Python daemon that I start with, when I'm developing a new daemon application.
Note that you'll need the
python-daemon
library. You can install it by:Then just start it with
./howdy.py start
, and stop it with./howdy.py stop
.I am afraid the daemon module mentioned by @Dustin didn't work for me. Instead I installed python-daemon and used the following code:
Running is easy
just for completeness here is samplemodule directory content
The content of moduleclass.py can be
The easiest way to create daemon with Python is to use the Twisted event-driven framework. It handles all of the stuff necessary for daemonization for you. It uses the Reactor Pattern to handle concurrent requests.
since python-daemon has not yet supported python 3.x, and from what can be read on the mailing list, it may never will, i have written a new implementation of PEP 3143: pep3143daemon
pep3143daemon should support at least python 2.6, 2.7 and 3.x
It also contains a PidFile class.
The library only depends on the standard library and on the six module.
It can be used as a drop in replacement for python-daemon.
Here is the documentation.