Monitoring web server status with a shell script

Recently, my VPS (Virtual Private Server) ran into some issues where it exceeded the maximum amount of RAM allotted under my subscription. When this happens, the web server software shuts down and does not restart until I manually restart it.

This is bad. I’m not always visiting my own web site, so it could be down for days without me knowing. Although I really need to identify what is using all the RAM, in the meantime I’ll settle for a monitoring system that will notify me when the server is down.

if curl -s --head | grep "200 OK" > /dev/null
    echo "The HTTP server on is up!" > /dev/null
    echo "The HTTP server on is down!"

cURL will let you retrieve a URL via the command line, and provides more options than Wget for a single URL. In this case, I used the silent switch to eliminate the status/progress output, and the head switch to retrieve only the document headers. The document header is then piped to Grep, which searches for the string “200 OK” (the HTTP status message for a successful request).

I send the result of that to /dev/null so that the output doesn’t appear on the screen.

If grep does find 200 OK, then I send a success message to /dev/null. This is largely unnecessary, but it is nice to leave in to test the script in a successful case–just remove the > /dev/null. If it doesn’t find 200 OK, then there is a problem. It might not mean, necessarily, that the web server is down, but it definitely indicates there is a problem that needs to be identified.

I added a call to this script to a crontab to run every 5 minutes. If there is no output, nothing happens. If there is output, the output is sent to me via e-mail, which, assuming I am checking my e-mail religiously, should reduce server downtime.

6 thoughts on “Monitoring web server status with a shell script”

  1. While a script may be a quick solution if you are running a single server, it is also scarce on info: you just get a message when it’s down. If you are running more servers and you care about a more detailed view, you need something that scales.

    We use for a visual overview. You can get detailed stats on your mobile, so you can always check what’s going on.

  2. On the last paragraph you mentioned:

    I added a call to this script to a crontab to run every 5 minutes. If there is no output, nothing happens. If there is output, the output is sent to me via e-mail.

    Can you share the code to run every 5 minutes as well as to send it to me via email?

  3. @Luluk,

    Adding the following to your crontab should work:

    */5 * * * * /path/to/bash/

    Any output of the script located at /path/to/bash/ will be mailed to your user account.

    You can edit your crontab by typing:

    crontab -e

    You could also save a file to /etc/cron.d/check_website with the following contents:

    */5 * * * * root /path/to/bash/

    Files in /etc/cron.d are in a similar format to your crontab, but need to additionally specify a user (in this case I’ve used root, but you could specify any valid local user). MAILTO will direct any output to the username specified.

  4. Hi

    I have used you script to monitor peoplesoft site and it is working fine problem is that some time https port is not listening so i wanted to have a script with https port . can you help me on this ?

Leave a Reply

Your email address will not be published. Required fields are marked *