Bash Script to Check Site Code Status with Email Alert

From Brian Nelson Ramblings
Revision as of 01:32, 2 May 2019 by Brian (Talk | contribs)

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/" | 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.