Today’s sysadmin tip: Using libslack daemon to daemonize a script

From time to time, I have to put a script in production that has to run in the background all the time. The script may have been written by some developer with too little time or knowledge on his hands to make it a classical full fledged UNIX style daemon. When this situation arise, I could edit the script a bit to make it log where I want, and put for example monit in charge of keeping it alive.

More elegant though, may be to pull good old libslack’s daemon from the hat. Here’s the from the home page blurb:

“Daemon turns other process into daemons. There are many tasks that need to be performed to correctly set up a daemon process. This can be tedious. Daemon performs these tasks for other processes. This is useful for writing daemons in languages other than C, C++ or Perl (e.g. /bin/sh, Java). ”

Among the the tedious things, you get are: It can keep a crashing script alive by restarting it at need. It can put STDOUT and STDERR to where you like it, that is log files or syslog. It does pidfile handling. All in all; the bookkeeping necessary to make a script run as a full fledged daemon. Though not exactly rocket science, it’s a very handy tool to have up your sleeve.

There are some old RPM packages available from the home page. I’ve brushed the spec file up a bit to make it ready for Red Hat derivates (Fedora, CentOS4/5, RHEL4/5). If I get the time, I’ll try to push it through to Fedora/EPEL. In my package, there is also an example Red Hat style LSB compliant init script that shows how to use daemon in a real life scenario.

Source and x86_64 binary rpms for el4/5 available at http://users.linpro.no/ingvar/daemon.

Leave a Reply