having fun with code

Check your server status with email notification

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:

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info