Bash Script to Check Site Code Status with Email Alert

From Brian Nelson Ramblings
Jump to: navigation, search

Bash Script to Check Site Code Status with Email Alert

Ever want a low cost way to check and make sure your site is loading correctly?

Here is a bash script that will send you an email alert when your site does not return a 200 status code

#!/bin/bash 
#######################################################################################
#Script Name    :alertsitedown.sh
#Description    :send alert email when 200 is not returned
#Author         :Brian Nelson
#Email          :brian[at]briansnelson.com
#######################################################################################
#Check the Status of a site
SC=$(curl --silent -IL $1 -A "Website Checker - https://briansnelson.com/Bash_Script_to_Check_Site_Code_Status_with_Email_Alert" | grep ^HTTP | awk '{print $2}'); 
##Now lets check if it gives a 200
if [ "$SC" != "200" ]; then 
#echo 'Site Error!!!!'
echo "Alert for $1 - Error Code $SC" | /usr/bin/mail -s "Alert for $1"  555555555[at]vzwpix.com
        else
#echo 'Site Working'
fi

After creating your script make sure to make it executable

chmod +x alertsitedown.sh

Usage

Very simple usage, set it up as a cronjob

crontab -e

Run every minute for best results

* * * * * /bin/bash /<location of script>/alertsitedown.sh https://briansnelson.com/

Replace our domain with the domain you wish to check, and there you go, a site down script checker.

Download script to your Server

It has come to my attention that you can not copy and paste the above code without an error to get around this you can download the script

wget https://briansnelson.com/stuff/alertsitedown.txt -O alertsitedown.sh ; chmod +x alertsitedown.sh

Just make sure to change the email in the script or it will not send, I like to get the alerts to my cell phone via a text message