I have been having issues I can’t narrow down that have been causing Apache to go into a conniption and the websites on my server to go down. The quick duct tape fix has been to login ot the server and restart Apache. This is a manual process though and the site could be down for a while before I get in to do the restart.
There are tons of website monitoring services out there, some free, that can alert you to such a failure. The “free” ones I have found though don’t allow you to do more frequent checking without upgrading to a paid plan. Furthermore, most are only “alert” services and cannot actually intervene and do anything.
I thought there must be a way to do this via a script and a cron job. After some digging around I wasn’t able to find a simple script that would do exactly what I wanted. However I found enough bits and pieces to cobble this together and after some testing it seems to work well.
/usr/bin/curl -s --head --request GET www.kiloroot.com | if ! grep "200 OK"; then
service apache2 restart
echo "$DATE - NOT OKAY, apache restarted" >> /var/log/apache2/custom-restarts.log
Basically the script runs the curl program against one of my sites. If part of the response to curl doesn’t include a “200 OK” message (which means that Apache has served the page successfully) then the script writes a log message with the date and restarts Apache. I then setup a root cron job to run this script every 5 minutes.
This is just a fancier form of automated duct tape… I realize that. Ultimately I need to get to the root of the problem and stabilize Apache. However it will provide me some peace of mind in the mean time. It should also give me a bit of forensic data as heretofore I really wasn’t sure how often my site was going down. After a week or so I should have a log of every time this script has had to restart Apache due to the site not responding.
Hopefully other folks will find this simple solution useful 🙂
BTW – Suggestions for improvement on this are always welcome! I think one could do a fair bit of automated monitoring and intervention with a more involved script.