Bash Script to Check Site Code Status with Email Alert

From Brian Nelson Ramblings
Revision as of 01:31, 2 May 2019 by Brian (Talk | contribs) (Created page with "===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...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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          :[email protected]
#######################################################################################
#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"  [email protected]
        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.