Installing Daemontools

From OpenBSD-Wiki

Jump to: navigation, search
Written for: OpenBSD Version 4.4

Contents

[edit] Why?

Daemontools is an alternate mechanism for controlling daemons.

OpenBSD normally operates by starting daemons from the /etc/rc.local file. Daemontools instead starts them from the daemontools daemon itself, started through by scripts beneath the /service directory. As with all mechanisms, it has advantages and disadvantages. The advantages include:

  • The ability to start and stop deamons dynamically.
  • The ability to add new daemons to a running system.
  • The ability to modify the run scripts for a daemon while the system stays up.
  • Automated restart of failing daemons (not always a good thing!)
  • Scriptable logging for those daemons that log to stdout
  • A rolling logging facility for daemons that log to stdout

Disadvantages:

  • Requires a bit of setup
  • Doesn't handle daemons that place themselves in the background very well.

[edit] The synopsis

  • The daemontools daemon, svscan, is indirectly started from the /etc/rc.local file.
  • svscan monitors the /service directory.
  • Each daemontools-controlled daemon has its own directory under /service. This is normally a symbolic link.
  • The directory under /service contains a file named run. This is executable, normally a shell script. Daemontools calls this file in order to start the daemon. The file normally exec's the daemon. The daemon should be run in foreground mode.
  • The directory may contain a subdirectory called log. If this directory exists and contains a run file in it, the output of the above command will be piped to the log's run command.

Which is all there is to it - quite straightforward. Daemontools contains a number of tools useful for running and logging as well.

[edit] Installation

[edit] Creating a run file

  • Fetch daemontools from the link at [1]. For the current version 0.76, the command is
Command: Fetch
wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
  • Install. This adds svscanboot to your /etc/rc.local and creates the /service directory.
Command: Install
mkdir -p /package
chmod 1755 /package
tar -xpzf daemontools-0.76.tar.gz -C /package
cd /package/admin/daemontools-0.76
package/install
  • Reboot to start
Command: Reboot
reboot
  • The daemontools FAQ asks you to report your success. I won't include a command here because it includes an email address.

Here is an example of /etc/rc.local (Daemontools part only)

# Add your local startup actions here.
#DAEMONTOOLS:
if [ -x /command/svscanboot ]; then
echo -n ' svscanboot'; csh -cf '/command/svscanboot &' 2>&1 >/dev/null
fi
echo '.'

Note the redirection of output (for not printing the process id) and the final "dot" after every command (to denote the end of local services). This is for a tidier startup log screen.

[edit] Examples

[edit] Adding a daemon

Daemontools has now been installed with no daemons.

To add a daemon, you need to create a directory and a run script for it. One useful daemon to run from daemontools is httpd because mucking with its /var/www/conf/httpd.conf can require restarting it on a regular basis, especially if you're like me and don't really understand it. Httpd can be run in the foreground and does its own logging, so its script is one of the most straightforward possible:

Command: Create the /service/httpd directory
mkdir /etc/httpd.daemon
ln -s /etc/httpd.daemon /service/httpd

Create a run command in the directory. Bernstein recommends edit the file and then using mv to rename it as run in an atomic operation:

Command: Create run command
vi /etc/httpd.daemon/run.new
# add contents
chmod 500 /etc/httpd.daemon/run.new
chown root /etc/httpd.daemon/run.new
mv /etc/httpd.daemon/run.new /etc/httpd.daemon/run

(This may be overelaborate, but you get the idea). Note in the run file that httpd is run with the -F option which keeps the daemon in the foreground. Many daemons have such an option. Note also that the script execs the httpd command, so that the executing shell is no longer kept around, not being needed.

Config File: run
#!/bin/sh
exec 2>&1
# Clean up left-over httpd locks
rm -f /var/www/logs/{ssl_mutex,httpd.lock,accept.lock}.*
exec /usr/sbin/httpd -F -DSSL

Once you have written this to the run file, httpd should start nearly immediately.

[edit] Creating a log file

Httpd does its own logging, but some applications might simply send their output to stdout (or stderr). One example is another of D. J. Bernstein's dnscache tool, a dns cache found at [2]. Creation of a log file involves creation of a log directory and a run file under there. You should use the same techniques to modify this file:

Command: Create log/run command
# create the log directory
mkdir /var/log/dnscache
chgrp gdnslog /var/log/dnscache
chmod 775 /var/log/dnscache

# edit the file
vi /etc/dnscache.service/log/run.new
# add contents
chmod 500 /etc/dnscache.service/log/run.new
chown root /etc/dnscache.service/log/run.new
mv /etc/dnscache.service/log/run.new /etc/dnscache.service/log/run
Personal tools