I wrote this little cron script (with help from Adam Aisen) so that I could keep track of our tomcat server. I wanted to be notified by email whenever our site was down (which is does occasionally due to out of memory errors). The shell script simply looks at the http status code and sends an email to the given email address if the site has anything but a “200 OK” code.
#!/bin/sh
# input validation
if [ ! -n "$1" ]
then
echo “You must enter a url as the first parameter”
echo “Usage: $0 <url> <emailaddress>[,<emailaddress>]”
exit
fi# input validation
if [ ! -n "$2" ]
then
echo “You must enter an email address to notify as the second parameter”
echo “Usage: $0 <url> <emailaddress>[,<emailaddress>]”
exit
fi# the default mail message
y=”The following services are broken! Oh noes! ”
z=0# fetch the page and look for the “200″ header
x=`wget -O – -q –save-headers $1 | grep “HTTP/1.1 200 OK”`
if [ ! -n "$x" ]
then
y=$y”\n\n$1 ”
z=1
fi# if one or more site above triggered an error, send the email
if test $z -eq 1
then
echo -e $y | mail $2 -s”Server status”
You call this script like so:
./serverstatus demo.openmrs.org/openmrs someuser@eflow.org
Now call this command as a cron job hourly/daily/minutely and you’re all set!