Check your server status with email notification

Jan 03 2009

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).

  1. #!/bin/bash  
  2.  
  3. # Query a supplied URL and output an error message if  
  4. # the status code was not 200. Can be used as a cron  
  5. # task to check if a server is up or not.  
  6.  
  7. # Ben Dowling – http://www.coderholic.com  
  8.  
  9. # The URL to query  
  10. if [ $# -ne 1 ]  
  11. then  
  12.     echo "Usage: $0 <URL>";  
  13.     exit;  
  14. fi  
  15.  
  16. url=$1;  
  17. response=$(curl -s -I -L $url | grep HTTP);  
  18.  
  19. status=${response#* }; # Strip off characters up to the first space  
  20. status=${status:0:3}; # Just use the 3 digit status code  
  21.  
  22. if [ "$status" != "200" ]  
  23. then  
  24.     echo "Error fetching $url. Status code '$status'";  
  25. fi

Nice.

Related Posts:

No responses yet

Leave a Reply