Linux

Tech Tip: Run services in Linux using daemontools

Learn how to run services in Linux using the daemontools package to ensure high availability.

The typical method for running services (or daemons) on a Linux system is to use initscripts--essentially, glorified shell scripts that start and stop services. While this approach works for the most part, a level of high-availability is missing. If a service stops on its own, it remains down.

Another way to run services is to use D. J. Bernstein's daemontools package. This runs a daemon called supervise that monitors all of the services it starts. If a service stops, supervise will restart it within five seconds, so the service is always up.

Using daemontools is fairly straightforward. It uses the /service directory hierarchy, with subdirectories named after the service beneath. The subdirectories contain a file called run, which is pretty much the same as an initscript.

The run script starts the service. If the service ever dies, supervise will reexecute the script to start it.

For example, if you want to run OpenSSH under supervise, create the directory /supervise/sshd. The contents of the run script should look like the following:

#!/bin/sh
exec /usr/sbin/sshd -D >/dev/null 2>&1

This starts sshd and prevents it from running in the background. Supervise needs all services it starts to run in the foreground. It doesn't impact the user at all; only supervise knows it's running in the foreground. But this enables supervise to better control the service.

If you want to stop the service, tell supervise you want the service marked as down, and instruct it to send the controlled daemon the TERM signal. Here's an example:

# svc -dx /service/sshd

This stops the service and flags it as down so supervise won't restart it within a few seconds of going down.

For more information on daemontools and to download the software, check out the daemontools Web site.

0 comments

Editor's Picks