Boilerplate code for a Python Unix daemon.
Python, 11 lines
1 2 3 4 5 6 7 8 9 10 11
import sys import os if os.fork()==0: os.setsid() sys.stdout=open("/dev/null", 'w') sys.stdin=open("/dev/null", 'r') while(1): # Daemon's main code goes here pass sys.exit(0)
Typo in the code: The parens are missing on the os.setsid() call.
Attack of the zombie processes... My understanding of *NIX daemon processes is that, to avoid creating a 'zombie' process, you must use the standard 'double-fork' technique -- you must fork an intermidiate child process, that then forks the desired child process. Comments?
no Zombie army... I just used:
And it resulted in no Zombie. Therefore, I guess, things work out right. It might be another issue if you do Shell Magick when starting the program, so the double fork could be added, just to be safe:
More complete implementation of forking a daemon. A more definitive/complete answer to the question "How do I get my program to act like a daemon?" is given by the following Unix Programming FAQ entry:
...which is fairly completely implemented by the following python cookbook entry (and its comments):
You need to double fork... A daemon is different than a background process. A background process is attached to a process group of a controlling terminal (your login shell). If your controlling terminal dies then you background jobs also die. I strongly suspect that your original code will die when you exit your login shell.
The reason you need to double fork is to disconnect your new process group from the parent process controlling terminal. Failing to do this does not cause a zombie. Instead, failing to do this causes the child process to be sent copies of the signals sent to the parent. That means when the parent login shell dies (HUP signal) or gets killed (KILL signal) those signals will be broadcast to the children.
The following code demonstrates a daemon in Python. When you start this up and a file ('/tmp/daemon.log') will slowly get filled with integers (once per second). You can logout, kill your shell, or whatever and the daemon will still run. It will not appear in the process list unless you do 'ps -x' to list processes without controlling terminals.
Note that you have to have correct permissions to do this. Some systems will not allow you to create a daemon. Some UNIXes have other mechanisms to do this. I'm pretty sure that this is portable.
I'm no authority. This is from memory. I may be wrong,
setpgrp vs. double fork. I believe that calling setpgrp() in your forked daemon obviates the need for a second fork. That is what I have always done and it seems to work fine.
Consider doing these only optionally. I hate it personally when programs insist on behaving like this normally; I use supervise to monitor all my daemon processes and it works wonderfully; no extra code in the daemons at all and good notification (and auto-restarting) of daemon death.
Simplified backgrounding... Could you not simply use the following to background the current process?
Here's the same code in a slightly different form:
Then, at the beginning of my script I can do: