Script to check if Website alive and restart Services if necessary

I was looking into a LAMP Server these days and had one effect that some pages became unresponsive over time. While the Error was not easy to detect and fix we decided to create a check script to check a certain URL and in case of a timeout server services would be restarted automatically. Not the bast possible way but maybe the most straight forward one.

Please adjust the service commands on the top to have the right restart commands inside.

#! /usr/bin/bash
function restart {
service nginx restart
service mariadb restart
service httpd restart
}

function check {
if [ $? -ne 0 ] ; then
echo "Error occurred getting URL $1:"
if [ $? -eq 6 ]; then
echo "Unable to resolve host"
fi
if [ $? -eq 7 ]; then
echo "Unable to connect to host"
restart;
fi
restart;
exit 1
fi

}

Call for this script looks like…

/scripts/check.sh http://www.URLTOCHECK.io

Cron like

*/15 * * * * /scripts/check.sh http://www.URLTOCHECK.io

Leave a Reply