Daemontools runs and manages persistent processes.

Installation

Debian

Debian changes several of the default daemontools directory locations in order to adhere to the filesystem heirarchy.

# aptitude install daemontools daemontools-run

Daemontools-run installs daemontools so that it runs at boot and is kept alive. Since daemontools must be running in order to manage other daemons, this is necessary.

The service directory is /etc/service, not /service.

Daemontools is not installed in /package.

Ubuntu

Ubuntu will start daemontools using upstart rather than init. Daemontools-run should install an upstart script, which can be started with:

# start svscan

CentOS

It seems easiest to compile daemontools from scratch according to djb's install instructions.

You'll first need to patch daemontools with the errno patch.

Commands

Daemontools is generally managed using the commands svc and svstat:

  • svc -u /service/mydaemon – Bring the daemon UP
  • svc -d /service/mydaemon – Bring the daemon DOWN
  • svc -t /service/mydaemon – TERM the daemon and restart it
  • svstat /service/mydaemon – Check the STATUS of the daemon

See the svc command reference for details.

Never run svscan, especially with a service directory as its argument. This command causes a new instance of daemontools to treat the provided directory as a daemontools /service directory that is monitored for daemons. This will cause chaos and prevent your daemons from running.

"Installing" daemons

Symlink the daemon's "service" directory (which contains its deamontools run script) into /service:

# ln -s /etc/mydaemon /service/
# svstat /service/mydaemon
/service/mydaemon: up (pid 1234) 2 seconds

Daemonizing

To create a daemon:

  • Create a service directory for your daemon
  • Within this directory, create a root owned executable script named run
  • This run script should exec your program, which must not detach from the terminal
  • To execute as a non-root user, use setuidgid exec setuidgid appuser myscript

See djb's daemontools faq for details. Logging uses a similar run script in the 'log' subdirectory.

Examples

A run script:

#!/bin/bash
exec 2>&1 # redirect STDERR to STDOUT for logging
exec setuidgid app \
     /home/app/.virtualenvs/bin/mydaemon \
         --config /etc/mydaemon.conf \
         --port 8080 \
         --log stderr

/log/run:

#!/bin/bash
# log to syslog
exec logger -p user.notice