Service monitoring with daemontools

This is a reference for setting up various daemons (services) under djb's excellent daemontools package, which provides tools for the supervision, monitoring, and control of system processes, as well as an assortment of other useful utilities. Some of these scripts make use of software included in djb's ucspi-tcp package.

The rationale for using daemontools and ucspi-tcp is available on djb's site. Suffice it to say that any competent systems engineer will immediately recognize the benefits of these packages.

I recommend the use of /var/spool/supervise to contain supervise directories. I've seen /etc/supervise used as well.

Shameless plug: I am available to set up any of this software on either a full-time employment or consulting/contracting basis. Feel free to inquire.

Shell scripts (run) for various services:

syslogd
#!/bin/sh
exec 2>&1
exec /usr/sbin/syslogd -n
    
klogd
#!/bin/sh
exec 2>&1
exec /usr/sbin/klogd -n
    
sshd (OpenSSH)
#!/bin/sh
exec 2>&1
exec /usr/local/sbin/sshd -D -e
    
crond
#!/bin/sh
exec 2>&1
exec /usr/sbin/crond -l10 -f
    
httpd (Apache)
#!/bin/sh
exec 2>&1
exec /usr/local/apache/bin/httpd -F
    
dnscache
#!/bin/sh
exec 2>&1
exec <seed
exec /usr/local/bin/envdir ./env sh -c '
  exec /usr/local/bin/envuidgid dnscache \
    /usr/local/bin/softlimit -o250 -d "$DATALIMIT" \
    /usr/local/bin/dnscache
'
    
tinydns
#!/bin/sh
exec 2>&1
exec /usr/local/bin/envuidgid tinydns \
  /usr/local/bin/envdir ./env \
  /usr/local/bin/softlimit -d300000 \
  /usr/local/bin/tinydns
    
pure-ftpd
#!/bin/sh
exec 2>&1
exec /usr/local/sbin/pure-ftpd -C 2 -E -d -4 -H -k 95 -u 100 -f none
    
mysqld
#!/bin/sh
exec 2>&1
exec /usr/local/mysql/libexec/mysqld -u mysql
    
postmaster (PostgreSQL)
#!/bin/sh
exec 2>&1
PREFIX=/usr/local/pgsql
exec /usr/local/bin/setuidgid postgres \
  $PREFIX/bin/postmaster -i -D $PREFIX/data
    
spamd (SpamAssassin)
#!/bin/sh
exec 2>&1
exec /usr/local/bin/spamd
    
ntpd
#!/bin/sh
exec 2>&1
exec /usr/local/bin/ntpd -n
    
imapd (Courier)
#!/bin/sh
exec 2>&1
PREFIX=/usr/local/courier
exec /usr/local/bin/envdir ./env \
  /usr/local/bin/tcpserver -v -R -H 0 143 \
  $PREFIX/sbin/imaplogin $PREFIX/libexec/authlib/authshadow \
  $PREFIX/bin/imapd Maildir
    
imapd-ssl (Courier)
#!/bin/sh
exec 2>&1
PREFIX=/usr/local/courier
exec /usr/local/bin/envdir ./env \
  /usr/local/bin/tcpserver -v -R -H 0 993 \
  $PREFIX/bin/couriertls -server \
  -tcpd $PREFIX/sbin/imaplogin \
  $PREFIX/libexec/authlib/authshadow \
  $PREFIX/bin/imapd Maildir
    
webproxy (micro_proxy)
#!/bin/sh
exec 2>&1
exec /usr/local/bin/setuidgid webproxy \
  /usr/local/bin/tcpserver -v -H -R 0 44444 \
  /usr/local/bin/micro_proxy
    
spam-stats
#!/bin/sh
exec 2>&1
exec /usr/local/bin/setuidgid spam-stats \
  /usr/local/bin/tcpserver -v -H -R 0 7300 \
  /usr/local/sbin/spamstats-stage1.pl
    
in.telnetd
#!/bin/sh
exec 2>&1
exec /usr/local/bin/tcpserver -v -R -H \
  -x telnetd/tcp.telnet.cdb 0 23 \
  /usr/sbin/in.telnetd
    
inetd
#!/bin/sh
exec 2>&1
exec /usr/local/bin/fghack /usr/sbin/inetd
    
dhcpd
#!/bin/sh
exec 2>&1
exec /usr/sbin/dhcpd -f eth1
    
pptpd (PoPToP)
#!/bin/sh
exec 2>&1
exec /usr/local/bin/pptpd -c /etc/pptpd.conf -f -l 10.1.1.1
    

Scripts to set up supervise directories:

makedir
#!/bin/bash

if [ "$2" == "" ]; then
  echo -e "\nUsage: $0 dir loguser\n";
  exit 100
fi

mkdir -p $1/log/main
chown $2 $1/log/main

echo -e "#!/bin/sh\nexec 2>&1" > $1/run
echo -e "#!/bin/sh\nexec setuidgid $2 multilog t ./main" > $1/log/run

chmod 0755 $1/run $1/log/run
    
makedir-auto
#!/bin/bash

if [ "$3" == "" ]; then
  echo -e "\nUsage: $0 dir loguser runcmd\n";
  exit 100
fi

mkdir -p $1/log/main
chown $2 $1/log/main

echo -e "#!/bin/sh\nexec 2>&1\nexec $3" > $1/run
echo -e "#!/bin/sh\nexec setuidgid $2 multilog t ./main" > $1/log/run

chmod 0755 $1/run $1/log/run
ln -s /var/spool/supervise/$1 /service