Skip to content

Instantly share code, notes, and snippets.

@alghanmi
Created May 24, 2012 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alghanmi/2784927 to your computer and use it in GitHub Desktop.
Save alghanmi/2784927 to your computer and use it in GitHub Desktop.
HTTP Status Check - Report any non-200 response to given email address
#!/bin/sh
WEB_PAGE=$1
ADMIN_EMAIL=$2
EMAIL_SIGNATURE="Sent by $USER@$(hostname) on $(date)"
# Get HTTP server response code
result=$(curl -s -I $WEB_PAGE | grep HTTP/1.1 | awk {'print $2'})
# Report a non-responsive server
if [ ! -n "$result" ]
then
echo -e "The server for the following web page is not responding:\n\t$WEB_PAGE\n\n$EMAIL_SIGNATURE" | mail -s "[URGENT] Server Not Responding" $ADMIN_EMAIL
exit 1
fi
# Report a non-200 (i.e. HTTP OK) response
if [ $result -ne 200 ]
then
service tomcat restart
echo -e "The following webpage produced a(n) HTTP $result error:\n\t$WEB_PAGE\nTomcat was restarted\n\n$EMAIL_SIGNATURE" | mail -s "HTTP Error $result on $(hostname)" $ADMIN_EMAIL
fi
@alghanmi
Copy link
Author

HTTP Status Check

Bash script to check the availability of a website or a specific web page via HTTP response codes. A success is measured by getting an HTTP 200 OK.

Usage Instructions

After downloading the script, make sure to make it executable:

chmod  755 http_status_check.sh

To run the script from the command line, you need to specify the webpage/website you are checking and the email to report errors to:

./http_status_check.sh http://www.usc.edu ttrojan@usc.edu

Notes & Future Improvements

  • If a page is using a redirect code like 301 or 302, the script will report an error regardless.
  • Self signed SSL certificates are not supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment