Monitoring php5-cgi with daemontools and spawn-fcgi

I found that, on my nginx installation, php5-cgi is particularly sensitive to php errors, and tend to crash when the code/configuration isn't very clean. The clean way to deal with that problem would be to treat the cause, and fix the code and the installation, but that doesn't remove the possibility of a crash. Therefore, monitoring the php5-cgi processes, launched by spawn-fcgi, is necessary. I will details how to do this on a Debian system using daemontools.

Understanding daemontools

Daemontools is a set of program, among which supervise and svc, that will be in charge for launching other programs. By doing so, daemontools keep a state of those processes and is able to restart them when they crash.

Debian ships too packages for daemontools: daemontools and daemontools-run.

# aptitude install daemontools daemontools-run

The second packages create the directory /etc/service and the script update-service. We are going to create our service directory somewhere on the system, say /etc/sv, and launch update-service to create a symlink into /etc/service and /var/lib/supervise.

Each monitored service will be composed of a directory located into /etc/sv containing a run script.

Monitoring the state of a process IMPOSES that the process does not fork, otherwise the forked process won't be monitored.

Installation of spawn-fcgi into /etc/sv can be done as follow:

server:# mkdir -p /etc/sv/spawn-fcgi
server:# cd /etc/sv/spawn-fcgi

The run script

In directory /etc/sv/spawn-fcgi, that we will call the service directory, we need a run script that will be read by the supervise program to start the processes. This run script contains the command line to start spawn-fcgi. An important argument to add is the -n switch, that will prevent spawn-fcgi to fork.

My run script for spawn-fcgi looks like the following:

server:/etc/sv/spawn-fcgi# cat run
#! /bin/sh
exec /usr/bin/spawn-fcgi -n -a 127.0.0.1 -p 9000 -u www-data -g www-data -C 5 /usr/bin/php5-cgi

Make this script executable and add this new service to the supervised services using update-service. supervise will then automatically start the process.

server:/etc/sv/spawn-fcgi# chmod +x run
server:/etc/sv/spawn-fcgi# update-service --add /etc/sv/spawn-fcgi spawn-fcgi
server:# ps -edf
[ ... ]
root     25995 19315  0 09:39 ?        00:00:00 supervise spawn-fcgi-jve
www-data 26231 25995  0 09:54 ?        00:00:00 /usr/bin/php5-cgi
www-data 26233 26231  0 09:54 ?        00:00:00 /usr/bin/php5-cgi
www-data 26234 26231  0 09:54 ?        00:00:00 /usr/bin/php5-cgi
www-data 26235 26231  0 09:54 ?        00:00:00 /usr/bin/php5-cgi
www-data 26236 26231  0 09:54 ?        00:00:00 /usr/bin/php5-cgi
www-data 26237 26231  0 09:54 ?        00:00:00 /usr/bin/php5-cgi

update-service has created a symlink into /etc/service and internal control files into /var/lib/supervise.

server# ls -l /etc/service
total 0
lrwxrwxrwx 1 root root 22  1 sept. 10:34 spawn-fcgi -> /etc/sv/spawn-fcgi
 
server# ls -l /var/lib/supervise/spawn-fcgi/
total 4
prw------- 1 root root  0  1 sept. 10:34 control
-rw------- 1 root root  0  1 sept. 10:34 lock
prw------- 1 root root  0  1 sept. 10:34 ok
-rw-r--r-- 1 root root 18  1 sept. 10:36 status

Control the service

The program svc can be used to control (start, stop) the service. It takes two arguments: the signal to send and the service directory.

To stop a service:

server:# svc -d /etc/service/spawn-fcgi

And to start it again:

server:# svc -u /etc/service/spawn-fcgi

Further reading

The location of the different configuration/data files can be confusing, this has been discussed has a Debian Bug available here.

en/ressources/dossiers/nginx/daemontools_spawnfcgi.txt · Last modified: 2011/03/16 01:30 (external edit)
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0