David Dollar

Using Ubuntu's Upstart To Keep Services Running

Ubuntu, since Intrepid, has replaced its init services with a daemon called Upstart. Similar to Apple's launchd, it provides a greater granularity to the events occurring on a modern computer.

We can use Upstart to start and stop services, and most importantly, to respawn them automatically if they die. The following is an Upstart script that I wrote to keep my IRC bouncer (bip) running.

The script has the following features:

  • Has a pre-start stanza to create the appropriate /var/run directory.
  • Starts and stops in the appropriate runlevels.
  • Uses sudo -u to run the process as the bip user.
  • Will respawn in case of process termination.
# /etc/event.d/bip
pre-start script
  mkdir -p /var/run/bip
  chown bip:bip /var/run/bip
end script

start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5

stop on runlevel 0
stop on runlevel 6

exec sudo -u bip /usr/bin/bip -n -f /etc/bip.conf -s /var/lib/bip