Automatically reboot web server and database if site is down

Sometimes for various reasons your web server may become hung. You may for example run out of resources on the server and there is not enough memory for various processes or maybe a script is running for too long.

Whatever the reason this little script can stand guard and restart the services in your absence. Replace <keyword> with a word that appears on your website if the site is running normally.

#!/bin/sh
curl localhost|grep <keyword>

if [ $? -ne 0 ] ; then
        /etc/init.d/mysqld restart
        /etc/init.d/httpd restart
fi

You can make it run every minute by adding the following line to your crontab with crontab -e

* * * * * /path/checkWebsite.sh

You can check that your script is running periodically using the following command.

tail -f /var/log/cron

 

Leave a Comment