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?
One more to thing to think about when daemonizing in python:
If your are using python logging and you want to continue using it after daemonizing, make sure to call
close()
on the handlers (particularly the file handlers).If you don't do this the handler can still think it has files open, and your messages will simply disappear - in other words make sure the logger knows its files are closed!
This assumes when you daemonise you are closing ALL the open file descriptors indiscriminatingly - instead you could try closing all but the log files (but it's usually simpler to close all then reopen the ones you want).
80% of the time, when folks say "daemon", they only want a server. Since the question is perfectly unclear on this point, it's hard to say what the possible domain of answers could be. Since a server is adequate, start there. If an actual "daemon" is actually needed (this is rare), read up on
nohup
as a way to daemonize a server.Until such time as an actual daemon is actually required, just write a simple server.
Also look at the WSGI reference implementation.
Also look at the Simple HTTP Server.
"Are there any additional things that need to be considered? " Yes. About a million things. What protocol? How many requests? How long to service each request? How frequently will they arrive? Will you use a dedicated process? Threads? Subprocesses? Writing a daemon is a big job.
Probably not a direct answer to the question, but systemd can be used to run your application as a daemon. Here is an example:
I prefer this method because a lot of the work is done for you, and then your daemon script behaves similarly to the rest of your system.
-Orby
After a few years and many attempts, now I realize that there is a better way than wanting to start, stop, restart a daemon directly from Python: use the OS tools instead!
In short, instead of doing
python myapp start
andpython myapp stop
, I do this to start the app:or
screen -dmS myapp python myapp.py
to start and detach it in one command.Then:
to attach to this terminal again. Once in the terminal, it's possible to use CTRL+C to stop it.
YapDi is a relatively new python module that popped up in Hacker News. Looks pretty useful, can be used to convert a python script into daemon mode from inside the script.
I modified a few lines in Sander Marechal's code sample (mentioned by @JeffBauer in the accepted answer) to add a
quit()
method that gets executed before the daemon is stopped. This is sometimes very useful.Here it is.
Note: I don't use the "python-daemon" module because the documentation is still missing (see also many other SO questions) and is rather obscure (how to start/stop properly a daemon from command line with this module?)