It can be absolute nightmare if you find out on Monday morning that your web server was down all weekend, and all your potential visitors would have been greeted with an error message! This has happened to me a couple of times recently (fortunately not on this site), so I came up with an automated checker. The shell script is below:
#!/bin/bash
# Query a supplied URL and output an error message if
# the status code was not 200. Can be used as a cron
# task to check if a server is up or not.
# Ben Dowling - http://www.coderholic.com
# The URL to query
if [ $# -ne 1 ]
then
echo "Usage: $0 <URL>";
exit;
fi
url=$1;
response=$(curl -s -I -L $url | grep HTTP);
status=${response#* }; # Strip off characters up to the first space
status=${status:0:3}; # Just use the 3 digit status code
if [ "$status" != "200" ]
then
echo "Error fetching $url. Status code '$status'";
fi
I then set this up as a cron task to run every 15 minutes, and to email me if the site doesn't respond with a HTTP 200 OK status:
*/15 * * * * /path/to/checkserver.sh "http://www.coderholic.com" | mail ben@coderholic.com