How do you create a daemon in Python?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Unforgiving Himalayas Looping
--
Chapters
00:00 How Do You Create A Daemon In Python?
00:42 Accepted Answer Score 209
01:12 Answer 2 Score 173
02:54 Answer 3 Score 44
03:35 Answer 4 Score 111
04:09 Thank you
--
Full question
https://stackoverflow.com/questions/4736...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #daemon
#avk47
ACCEPTED ANSWER
Score 209
Current solution
A reference implementation of PEP 3143 (Standard daemon process library) is now available as python-daemon.
Historical answer
Sander Marechal's code sample is superior to the original, which was originally posted in 2004. I once contributed a daemonizer for Pyro, but would probably use Sander's code if I had to do it over.
ANSWER 2
Score 173
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
chrootgaolset UID, GID, working directory, umask, and other process parameters appropriately for the use case
relinquish elevated
suid,sgidprivilegesclose 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,stderrsince 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.
ANSWER 3
Score 111
Here's my basic 'Howdy World' Python daemon that I start with, when I'm developing a new daemon application.
#!/usr/bin/python
import time
from daemon import runner
class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
    def run(self):
        while True:
            print("Howdy!  Gig'em!  Whoop!")
            time.sleep(10)
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
Note that you'll need the python-daemon library.  You can install it by:
pip install python-daemon
Then just start it with ./howdy.py start, and stop it with ./howdy.py stop.
ANSWER 4
Score 44
Note the python-daemon package which solves a lot of problems behind daemons out of the box.
Among other features it enables to (from Debian package description):
- Detach the process into its own process group.
 - Set process environment appropriate for running inside a chroot.
 - Renounce suid and sgid privileges.
 - Close all open file descriptors.
 - Change the working directory, uid, gid, and umask.
 - Set appropriate signal handlers.
 - Open new file descriptors for stdin, stdout, and stderr.
 - Manage a specified PID lock file.
 - Register cleanup functions for at-exit processing.