Archive for July, 2013

Maintaining a handful of websites can be stressful. Even more so, not knowing when one of those sites goes down.

There are services such as SiteUptime that will do this for you out of the box, however there are efficient, free ways of doing this yourself.

1. You have permission to create and run bash (shell) scripts on your server.
2. You have permission to create and schedule cronjobs on your server.
3. You have permission to send emails from your server.

Ok, let’s get started.

1. Create a file on your server called (the location doesn’t matter, however I recommend keeping it outside of publicly accessible folders).
2. In, paste the following:


SITESFILE=sites.txt #list the sites you want to monitor in this file
EMAILS="," #list of email addresses to receive alerts (comma separated)

while read site; do
    if [ ! -z "${site}" ]; then
        CURL=$(curl -s --head $site)
        if echo $CURL | grep "200 OK" > /dev/null
            echo "The HTTP server on ${site} is up!"

            MESSAGE="This is an alert that your site ${site} has failed to respond 200 OK."

            for EMAIL in $(echo $EMAILS | tr "," " "); do
                SUBJECT="$site (http) Failed"
                echo "$MESSAGE" | mail -s "$SUBJECT" $EMAIL
                echo $SUBJECT
                echo "Alert sent to $EMAIL"

3. In the file, change the EMAILS variable to a comma separate list of email addresses you wish to send the notification to. For Example: EMAILS=","

4. Create a file named sites.txt in the same folder as In this file, create a list of complete URLs you wish to monitor.

Make sure your domain names are absolutely correct. Double check whether your URLs use WWW or Non-WWW url structures

5. Test the script. Login to your server using SSH, and run the bash file using the following command:

6. If done correctly, you will see something like this:

The HTTP server on is up!
The HTTP server on is up! (http) Failed
Alert sent to
Alert sent to

7. To have this script run automatically (say every 5 minutes), you can setup a cronjob. You can read more on how to do this by reading HowTo: Add Jobs To cron Under Linux or UNIX

Once setup, your cronjob will run at the interval you set, and you will receive an email if your site doesn’t response.

Keep in mind, if your sites are on the same server as this script runs and the cause of downtime is your server going down, you may not receive the alert email. This should alert you if your apache (httpd) process fails as this script does not rely on that process to run.