Simple HTTP Status Monitor Using Curl


Using some output redirection and the --write-out parameter we can produce a script that simply outputs the status code of a curl request.

Simple script

This script can then be used as the basis for a simple monitoring of a URL. See the example below.

#! /bin/bash
# Description: Returns the HTTP response code obtained by curl on the URL using the specified HTTP method
# Usage: script.sh GET https://example.org
# Usage: scripts.sh POST https://example.org
# Author: marco@kamner.eu

METHOD=$1
URL=$2

curl --silent --output /dev/null --write-out "%{http_code}" --request $METHOD $URL

This script can easily be adapted to output other information using the variables available to --write-out.

Continual monitoring

Info

This script can also be found in my public scripts repository, where I might change it over time.

This can easily be adapted to a continual monitoring script, which can for example be useful to see URL health during a update.

You could call it like this: ./check.sh GET https://example.org/healthcheck 5 | tee -a example-health.log

#! /bin/bash
# Description: Returns the current timestamp and HTTP response code obtained by curl on the URL using the specified HTTP method, repeating every DELAY seconds
# Usage: script.sh GET https://example.org 5
# Usage: scripts.sh POST https://example.org 1
# Author: marco@kamner.eu

METHOD=$1
URL=$2
DELAY=$2

while true; do
    TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
    echo "$TIMESTAMP $(curl --silent --output /dev/null --write-out '%{http_code}' --connect-timeout 5 --request $METHOD $URL)"
    sleep $DELAY
done

See also