Write a shell script to check spawn-fcgi’s status

After I built this site with nginx and spawn-fcgi, the process of spawn-fcgi crashed for several times, and I have no idea about how it happend. So I decided to write a shell script to check if the process is still running and use crontab to run this script regularly.

Create the shell script “/usr/bin/check-fastcgi”:

#!/bin/bash
timestr=`date`
echo $timestr:>>/srv/www/log/fcgi.log
status=`netstat -anut | grep 127.0.0.1:9000`
if [ "$status" ]; then
echo “fastcgi: running”>>/srv/www/log/fcgi.log
else
cmdStr=`/etc/init.d/init-fastcgi restart`
echo $cmdStr>>/srv/www/log/fcgi.log
fi
Make script executable:
chmod +x /usr/bin/check-fastcgi
Then create a crontab file “crontab_check”:
x/5 x x x x check-fastcgi
This sentence means to run the script every five minutes. Add it to cron schedule:
crontab crontab_check
It’s done. Use “crontab -l” to check the schedule list and there was a new task I just added.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>