I wanted to do this for a while and never really sit down and code it. Yesterday I came across this pretty nice script by Ben from Coderholic which does exactly that: check your server status and send you an email if it is other than 200 (OK).
-
#!/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
Nice.