Need to write a daemon in Python?

Then you need to check out this link: It incorporates creating a pid file, controlling the daemon (start, and stop), and doing the work of actually daemonizing and installing signal handlers. It’s nice snippet of code to work from if you ever need to create your own daemon. I needed to do something like this for work, and used the Python Cookbook recipe to daemonize, but took a quick look at this for the signal handlers to install and ignore.

My version also incorporated logging via syslog, which is very, very easy… once you know the trick: the address of the local machine is /dev/log, if you don’t want to connect via UDP. Most Linux distributions don’t accept syslog data over the network by default. You have to explicitly enable that (which is A Good Thing). However, it doesn’t match the default in logging.handlers.SysLogHandler(). Use /dev/log as the address, and it’ll output to /var/log/messages, as you would expect.

Thanks to Ulrich Berning for posting this snippet. I’ll be keeping it around for future use!