Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

How to auto-restart spawn-fcgi

Michel Nadeau, 09-09-2008
If like us you are running Nginx, PHP and spawn-fcgi, you may have noticed that sometimes PHP/spawn-fcgi crash or hang up. One of the key reasons appears to be warnings and fatal errors in PHP - but so far we didn't come up with any solution to prevent PHP downtime.

As a temporary solution we developed a spawn-fcgi auto-restart BASH script that runs every minute in the cron.

1 - What does the script?

The script does pretty simple checks:

- Is PHP running? If no, we need to start it
- If running, is PHP accepting connections? If no, we need to restart it
- Send an email if we started/restarted PHP

2 - The script

Here's the script:

fix.sh
#!/bin/sh
# (bof)

# Base directory
cd $(dirname $0)
base=$(pwd)

# Temp file
tmp="/tmp/fix.$$"

# Output to file?
[ -n "$1" ] && exec 1>$tmp 2>&1 3>&1

echo ""

# Settings
php="/usr/local/bin/php"

# Init
must_kill=0
must_start=0
must_email=0

# Running?
pids=$(ps ax | grep "$php$" | grep -v "grep" | cut -d"?" -f1 | tr -d " ")
if [ "$pids" != "" ]
then
a=$(telnet localhost 8888 /dev/null | grep "^Connected to localhost")
if [ "$a" = "" ]
then
echo "PHP is running but NOT accepting connections!"
must_kill=1
must_start=1
else
echo "PHP is running and accepting connections!"
fi
else
echo "PHP is stopped!"
must_start=1
fi

# Must kill?
if [ $must_kill = 1 ]
then
echo "Stopping PHP..."
for pid in $pids
do
kill $pid >/dev/null 2>&1
done

# Wait for all pids to disappear
pids="!"
while [ -n "$pids" ]
do
pids=$(ps ax | grep "$php$" | grep -v "grep" | cut -d"?" -f1 | tr -d " ")
done
echo "PHP was stopped"
must_email=1
fi

# Must start?
if [ $must_start = 1 ]
then
echo "Starting PHP..."
/usr/local/etc/rc.d/spawn-fcgi.sh
must_email=1
fi

echo "Done!"
echo ""

# Must email?
if [ $must_email = 1 -a -n "$1" ]
then
$base/sendEmail -u "PHP was restarted on name_of_your_server" \
-f from@domain.tld \
-t to1@domain.tld \
to2@domain.tld \
-s your_mail_server:25 \
-o message-file=$tmp >/dev/null 2>&1
fi

# Clean up
rm -rf $tmp

# (eof)

3 - Adjusting the script

To get the script working, you will need to adjust some things:

- the php variable: adjust it so it points to your php-cgi, or whatever php binary you're using with spawn-fcgi
- the FastCGI port 8888: change it for yours (normally 9000)
- the sendEmail line: change the from, to, server, subject, etc. And do not forget to download sendEmail and put it along with the fix.sh script (http://caspian.dotconf.net/menu/Software/SendEmail/)
- in the "Starting PHP" block, change the path for spawn-fcgi.sh (or whatever script you use to launch spawn-fcgi)

4 - How to use the script?

There is 2 ways of using this script:

- /path/to/fix.sh: no parameter, will run/output on screen. Do not send an email even if needed
- /path/fo/fix.sh "whatever": with parameter, will run/output in a file (no screen output). Sends an email if needed. You should use this method in the cron.

Cron example:
# Restart PHP if needed
* * * * * /root/fix/fix.sh "output"

5 - Conclusion

This script is definitely not a permanent solution. It is only a temporary fix to avoid PHP downtimes (max 1 minute of downtime). Expect another post very soon when we will figure out a definitive solution to the problem.

eyecool, 09-08-2009
Did you ever figure it out? Is php-fpm the answer or does it crash too?

jefferai, 12-29-2009
Use monit. Put the following in your /etc/monitrc and make sure that you set the timeout interval to something sufficiently low (default is two minutes, which is a lot of downtime in some cases):

check host localhost with address 127.0.0.1
start program = "/etc/init.d/spawn-fcgi.nginx start"
stop program = "/etc/init.d/spawn-fcgi.nginx stop"
if failed port 9000 then restart

djik, 03-08-2010
hi, did you figure it out finally ?
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions