Recipe for creating a Linux daemon from a Python, Ruby, or NodeJS script.
Service managers like runit, SysV init, and upstart work great to start daemons but it can be tricky to stop daemons. The requirements of a well behaved daemon are pretty onerous to add to your script. The python-daemon package can handle this for you, but I prefer to keep the daemon logic out of my script as much as possible.
This repo is a demo using runit to run a python script as a daemon that can be cleanly started and stopped and is automatically restarted when it exits. The python script is not burdened with daemon logic.
Example daemon that just prints from time to time. It knows very little about its daemonic nature: it writes its pid to a file, that's it.
runit is simple and reliable. It restarts your daemon automatically if it exits, which is really useful. This frees your daemon from having complicated try/catch logic that tries to keep it running at all costs while still reporting errors. With runit, just let your daemon exit if it hits a hard error: runit will restart it.
This starts your daemon and sends its standard output to the system logger.
This is a runit hook script that is called whenever your service is stopped. Why do we need it? The loopy daemon runs as two processes, and runit only knows about the parent process that it started. The control/t
script stops the child python process cleanly.
Control hooks are somewhat buried in the docs, but basically runit lets you run a custom script for each action it takes on a daemon.
This hook is run when your service starts. It is a symlink to t
. It tries to the process and pid file if it is already running. It almost never will be running, so u
is mostly an extra precaution.
That's it. You can see it in action with docker or use the snippets above in your own daemons.
docker build -t loopy --rm .
docker run -it loopy /sbin/my_init -- bash -l
You'll see the loopy output every few seconds. After 25 seconds loopy will exit and be restarted automatically. You can do sv start loopy
and sv stop loopy
. You can also do ps -ef
to see that there is one loopy python process when it is started and zero loopy processes when it is stopped.
- runit
- SO thread
- python-daemon - this works well if you prefer to have your script contain the daemon logic
- phusion/baseimage - a great way to start the Dockerfile for your image