script to automatically test if a web site is available

Well… The most simple script, I cam write:

/usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || echo "The site is down" | /usr/bin/mail -v -s "Site is down" your@e-mail.address

Add it to cron as:

* * * * * /usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null  | grep "Normal operation string" || echo "The site is down" | /usr/bin/mail -v -s "Site is down" your@e-mail.address

But it is too simple to tell you what the problem is if it exists.

UPD: Now this one-liner checks for a specific string on the page (“Normal operation string”), which should appear only on normal operation.

UPD2: A simple way to send the error page in the e-mail:

/usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | grep "Normal operation string" || /usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null | /usr/bin/mail -v -s "Site is down" your@e-mail.address

It’s minus is that the page is re-requested in case of first test failure. This time the request may be successful and you won’t see the error. Of course, it is possible to store the output and send it as an attachment, but it will make the script more complex.

Leave a Comment