How to run lighttpd under upstart
Upstart is Ubuntu’s init.d replacement. It greatly simplifies writing init.d scripts and has a great respawn feature similar to daemontool’s supervise or monit. And it comes with Ubuntu by default.
For some reason, almost no one uses upstart. Even Ubuntu’s services use traditional /etc/init.d scripts instead of upstart scripts. I think this might be due to upstart’s non-existent documentation. There is no man page for upstart, and multiple people I know who have read the online docs somehow missed the three important commands that control upstart jobs: /sbin/start, /sbin/stop, and /sbin/status!
Here is how it works: put an upstart script in /etc/event.d. Let’s call it /etc/event.d/foo. This script is now immediately available under upstart. Just type sudo start foo. That’s it.
I converted Ubuntu’s /etc/init.d/lighttpd script to a much shorter upstart script. The big advantage of this is upstart will restart lighttpd if it dies for some reason. This is what the upstart script looks like:
#this is an upstart script that starts lighttpd start on runlevel 2 start on runlevel 3 start on runlevel 4 start on runlevel 5 stop on runlevel 0 stop on runlevel 1 stop on runlevel 6 respawn exec sudo -u www-data lighttpd -D -f /etc/lighttpd/lighttpd-infobase.conf
That’s it! Save this script as /etc/event.d/OL-lighttpd, and then type sudo start OL-lighttpd. You can kill off the lighttpd process and it will get restarted.
If you want to configure your lighttpd to write out a pid file, you can use pre-start and post-stop script to prepare and clean up the pid file:
#this is an upstart script that starts lighttpd start on runlevel 2 start on runlevel 3 start on runlevel 4 start on runlevel 5 stop on runlevel 0 stop on runlevel 1 stop on runlevel 6 pre-start script #make sure there is a place to write the pid file (optional): mkdir -p /var/run/lighttpd > /dev/null 2> /dev/null chown www-data:www-data /var/run/lighttpd chmod 0750 /var/run/lighttpd end script respawn exec sudo -u www-data lighttpd -D -f /etc/lighttpd/lighttpd-infobase.conf post-stop script #remove pid file (optional) #add server.pid-file = "/var/run/lighttpd/lighttpd.pid" to lighttpd.conf file to have it generate the pid file rm -f /var/run/lighttpd/lighttpd.pid end script
If you want to stop lighttpd, just type sudo stop OL-lighttpd. You can also type sudo initctl list for a list of all jobs under upstart.
Filed under: code code, howto, lighttpd, support, ubuntu, upstart · 0 Comments
