Auto restart lighttpd on crash

To restart lighttpd if it went down or crashed, create a file check_lighttpd.sh

mkdir /usr/serverok
vi /usr/serverok/check_lighttpd.sh

With the following content

#!/bin/bash

/usr/bin/wget --tries=1 --timeout=30 -O /dev/null http://localhost:80/up.html

if [ $? -ne 0 ]; then
(
killall -9 lighttpd;
killall -9 gam_server;
killall -9 php-cgi;
/etc/init.d/lighttpd stop;
mv /var/log/lighttpd/access.log /var/log/lighttpd/access.$(date +%Y%m%d_%H).log
mv /var/log/lighttpd/error.log /var/log/lighttpd/error.$(date +%Y%m%d_%H).log
/etc/init.d/lighttpd start;
echo 'restarting lighttpd' `ps aux` | mail -s "`hostname` restarting lighttpd `who | awk '{print $6}'`" you@yourdomain.extn
datetime=`date "+%Y%m%d %H:%M:%S"`
echo $datetime "failure">>/var/log/lighttpd/check.log
)
fi

Make it executable

Advertisement

chmod 755 /usr/serverok/check_lighttpd.sh

Create a file up.html on web root of your web site

echo "up" > /path/to/docroot/up.html

Now set a cronjob to run every 5 minutes

crontab -e

Add following cronjob

*/5 * * * *  /usr/serverok/check_lighttpd.sh  1> /dev/null 2> /dev/null
Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement