XtremeDocumentStudio
.NET
PDFOne
.NET
XtremeDocumentStudio
(for Java)
PDFOne
(for Java)
Gnostice Document Studio
Delphi
eDocEngine
VCL
PDFtoolkit
VCL
StarDocs
Web APIs

Gnostice Blog

Thursday, December 03, 2015

A Linux BASH script to check whether a website is online

And shoots an e-mail if it goes offline.

By V. Subhash

Despite the best of preparations and precautions, websites do go down sometimes. Here is a script to regularly check if a website is online and if it ever goes offline, send a mail to a pre-configured email address. The script will be executed the script every half an hour. It is placed in a simple text file and executed by bash.

watch -n 1800 bash check-website.txt http://www.test-example-website.com email@example.com

Before executing the script, msmtp and curl packages were installed. msmtp* was configured to use an existing SMTP server to send the mail. The script first uses curl to do a http HEAD request to a test website to check if an Internet connection exists. If that HEAD request is successful, then another http HEAD request is made to the URL specified in the first argument. When the URL has gone offline, the email in the second argument will be mailed.

_WebsiteHttpStatus=""
_WebsiteHttpStatus=""
_WebsiteStatus=""

_RedirectionCheck=""
_RedirectionHeader=""
_RedirectionUrl=""
_RESOLVEERROR="Could not resolve host:"


function mailItToSupport {
echo "`date --date=now +'%Y-%m-%d %H:%M'`: \"$1\" is not okay - $3" >> check-website.log
printf "\"$1\" is not okay - $3"

echo "To: $2" > mailtext.txt
echo "From: do-not-reply@example.com" >> mailtext.txt
echo "Subject: Website not okay - $1" >> mailtext.txt
echo "Reply-To: support@example.com" >> mailtext.txt
echo "$3" >> mailtext.txt
echo "" >> mailtext.txt

cat mailtext.txt | msmtp $2
}

function getWebsiteHttpStatus { #url
_WebsiteHttpStatus=`curl --silent --head $1 | head --lines=1 | sed 's/\r//'`
}


function checkForRedirection { #url
sRedirectionHeader=`curl --silent --head $1 | grep 'Location: '`
_RedirectionCheck=`echo $sRedirectionHeader | wc -l`
}


function getRedirectionUrl { #url
_RedirectionHeader=`curl --silent --head $1 | grep 'Location: '`
_RedirectionUrl=`echo $_RedirectionHeader | awk '{ print $2}' | tr -d '\r'`
#echo $_RedirectionUrl
}


function getWebsiteStatus { #url
getWebsiteHttpStatus $1
#echo $_WebsiteHttpStatus

# No connection or website silent
if [ "$_WebsiteHttpStatus" == "" ]
then
_WebsiteStatus="notalk"

# URL does not exist
elif [ "$_WebsiteHttpStatus" != "${_WebsiteHttpStatus%$_RESOLVEERROR*}" ]
then
_WebsiteStatus="nodns"
elif [[ "$_WebsiteHttpStatus" = "HTTP/1.1 200 OK" || "$_WebsiteHttpStatus" = "HTTP/1.1 302 Found" ]]
then
_WebsiteStatus="fine"
elif [[ "$_WebsiteHttpStatus" = "HTTP/1.1 404 Not Found" ]]
then
_WebsiteStatus="broken"
else

checkForRedirection $1

if [ "$_RedirectionCheck" == "1" ]
then
_WebsiteStatus="redirected"
else
_WebsiteStatus="dead"
fi
fi
#echo $_WebsiteStatus
}


i=1
n=4 # redirection limit
sUrlToCheck=$1
while [ $i -le $n ]
do
printf "\nChecking $sUrlToCheck..."
getWebsiteStatus $sUrlToCheck

if [ "$_WebsiteStatus" == "nodns" ]
then
printf "\n\t Link could not be resolved.\n"
break

elif [ "$_WebsiteStatus" == "notalk" ]
then
printf "\n\t Link could not be connected to.\n"
break

elif [ "$_WebsiteStatus" == "fine" ]
then
printf "\n\t Link is fine.\n"
break

elif [ "$_WebsiteStatus" == "broken" ]
then
printf "\n\t Link is broken.\n"
mailItToSupport $1 $2 "Link is dead - $_WebsiteHttpStatus"
break

elif [ "$_WebsiteStatus" == "dead" ]
then
printf "\n\t Link is dead.\n"
mailItToSupport $1 $2 "Link is dead - $_WebsiteHttpStatus"
break

elif [ "$_WebsiteStatus" == "redirected" ]
then
getRedirectionUrl $sUrlToCheck
printf "\n\t Redirected to $_RedirectionUrl"
sUrlToCheck=$_RedirectionUrl

else
printf "\nThere is an error in this script\n"
break
fi

let "i++"
done

MSMTP Configuration

MSMTP requires mail server settings in the ~/.msmtprc file. Here is a sample.

defaults

logfile ~/Scripts/msmtp.log

# SSL mail server configuration
account gmail
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
host smtp.gmail.com
port 587
auth on
from example@gmail.com
user example@gmail.com
password WhatPassword


# Non-SSL mail server configuration
account nossl
tls off
host smtp.example.com
port 25
auth login
from donotreply@example.com
user donotreply
password TopSecret

#Default account
account default : nossl

After specifying the settings in the file, make it read-only. If you are using a SSL SMTP server, such as Gmail, then ca-certificates should be installed.

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2018 Gnostice Information Technologies Private Limited. All rights reserved.