Sunday, April 27, 2008

check if a website is responding script

recently i needed to check if a website was responding, and if not, page myself (by sending an email to my pager). Here is the unix shell script i used to make this happen.

Required programs:
wget
working mail (i use xmail on solaris 8 in this script)

script:

#!/bin/sh
## Script will check if the "host" is up, if the host is down, send an email
## You should cron it every 5 mins or so.

#uncomment to debug:
#set -x

## change these:
host="http://dogself.com"
email="my-pager-number@skytel.com,my-real-email@gmail.com"

## locations of stuff:
mailx="/usr/bin/mailx"
wget="/usr/bin/wget"
log="/path/to/a/writable/log/file.log"

now=`date '+%m/%d/%Y %H:%M:%S'`
rm ${log}
#when checking connection, do 2 tries, and time out in 7 seconds
${wget} -O /dev/null -t 2 -T 7 ${host} -o ${log}
grep "saved \[" ${log}
if [ $? = "1" ];
then
echo "site:[${host}] is down"
${mailx} -s "PRODUCTION is DOWN at ${now}" ${email} < ${log}
else
echo "site:[${host}] is up"
fi

1 comment:

  1. Nice script. Thanks. I used sendemail instead of mailx, but otherwise pretty much word for word and it works just fine.

    Richard

    ReplyDelete