I'm new to daemons so apologies if this is a newbie question.
In several other answers (for example, this question) people suggested the python-daemon package was the way to go because it fully implements the PEP 3143 standard.
Unfortunately, python-daemon is a bit light on documentation (or more likely I am a bit light on knowledge / experience... ;) ), and I think I am probably missing something really basic. Here's what I'm doing:
I have the following:
import daemon
logfile = open('daemon.log', 'w')
context = daemon.DaemonContext(stdout = logfile, stderr = logfile)
context.open()
with context:
do_something_1()
do_something_2()
Question: How do I set up a daemon with python-daemon, how can I start it and stop it?
Side notes:
I'm basically taking a wild guess about how / whether the .open()
method should be used here -- docs were not real clear on this point. Same thing seems to happen whether I include it or not.
So, now what do I do? When I try running this file, eg:
python startConsumerDaemons.py
it appears to run do_something_1()
, but not the second. And, it appears to leave the program attached to the terminal window. IE, stdout isn't redirected, and when I close the terminal window the process is killed. So, I'm pretty sure I'm doing something wrong here... what should I be doing differently?
And, lastly, once I get the daemon running, how do I stop / restart it (for example if I make changes to the underlying code)?
The
daemon.DaemonContext
constructor accepts alockfile
parameter. Use a lockfile library that will record the PID of the process, such aslockfile.PIDLockFile
.Then, the PID of the process is found simply by reading the content of the named PID file. Use that PID to send signals to your running daemon.
A full example is available here.
You should be able to better understand the inner workings of python-daemon.
Moreover the code provided also gives an example of an init script to simply start/stop the daemon. However, you can start/stop it simply by calling the original function again with the argument stop:
Here is what I have, that works for me. It also has a sysv init script. Repo is at GitHub, and I also have a brief blog post with links to other possible solutions I found.
There can only be one daemon process running: that is managed by the PID lock file, like most other Linux daemons. To stop it, do
To see if it is running:
Using the pidfile submodule, the PID file is managed automatically. When the daemon is stopped, the pidfile is cleared up. Please see the linked GitHub repo for the init script.
Here's the Python daemon code:
For completeness' sake, here is the init script. Note that "kill" is really just a method for sending a POSIX signal -- see man page for signal(7) for an overview. The python-daemon context will catch the signal, terminate the process cleanly closing file descriptors, and delete the PID file automatically. So, it really is a clean termination.
You can write your code to catch SIGUSR1 or something similar, in order to do a reload of the daemon config. There is no advantage to writing Python stop the daemon.
A useful documentation is still missing for the module "python-daemon". I personally gave up about using it, and now I successfully use Sander Marechal's daemon code referenced in this answer.
I slightly modified it in order to be able to do things when you call
python testdaemon.py stop
.Here is the code.
Sample usage:
On linux, you can stop the Daemon by running:
and find the PID that corresponds to your daemon and then just kill the process.
The
daemon.DaemonContext
constructor accepts alockfile
option. Use a lockfile library that will record the PID of the process.The library originally recommended the
lockfile.PIDLockFile
class, but that library is now deprecated without a good replacement. But you can implement another object with the same semantics.Then, the PID of the process is found simply by reading the content of the named PID file. Use that PID to send signals to your running daemon.