If you are running one or a few your websites, you definitely would like to avoid any access issue to your site. However, you cannot stop it happens. When your website does have some access issues, you need to know at the first place before any of your visitors, and then you can have some time to fix it.

A straightforward way to monitor your website is writing a small script that can send a request to your site and check if the response is what we are expected. If you cannot hear a response or the response is something you are not acceptable, then an issue can be identified and send a notification to your preferred channel.

This is a very simple python script that we can use

import urllib
import json
import urllib2
import time

def notify_slack(options):
    wekbhook_url = 'my-slack-webhook-url'
    data = json.dumps(options)
    request = urllib2.Request(wekbhook_url, data)
    urllib2.urlopen(request)

def check_url(url, expected_status = 200):
    try:
        status = urllib.urlopen(url).getcode()
    except Exception as e:
        status = str(e)

    if status != expected_status:
        notify_slack({
            "attachments": [
                {
                    "title": url,
                    "title_link": url,
                    "text": "This url seems like have some issue",
                    "color": "warning",
                    "fields": [
                        {
                            "title": "Expected Status",
                            "value": expected_status,
                            "short": False
                        },
                        {
                            "title": "Received Status",
                            "value": status,
                            "short": False
                        }
                    ],
                    "ts": time.time()
                }
            ]
        })

check_url("https://your-own-url")
check_url("https://2nd-your-own-url")

This script would access the URL you defined, and then it the HTTP response status code is not equal to 200, then would send a notification to my slack channel.

After we got the script in the server, let's setup the cron job for this script, type this in your console

crontab -e    # edit your cron jobs

Adding this line to your cron job lists

* * * * * python /path/to/cron/healthcheck.py > /dev/null 2>&1

Finally, you have set up a cron job to monitor your website, and it would run every minute. So if your sites have any issue, you will be notified within 1 minute at the latest.