BASH

Check Website Uptime and Send Alert

A Bash script to continuously monitor a website's availability, checking its HTTP status code, and sending an email alert if it's detected as down.

#!/bin/bash

URL="https://example.com"
ALERT_EMAIL="[email protected]"

while true; do
    HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}
" "$URL")

    if [ "$HTTP_STATUS" -ne 200 ]; then
        echo "$(date): $URL is DOWN! Status Code: $HTTP_STATUS" | mail -s "Website Down Alert: $URL" "$ALERT_EMAIL"
    else
        echo "$(date): $URL is UP. Status Code: $HTTP_STATUS"
    fi

    sleep 60 # Check every 60 seconds
done
How it works: This script uses `curl` to fetch only the HTTP status code of a specified URL. If the returned status code is not 200 (indicating success), it sends an email alert to the configured address using the `mail` command (which requires a local mail transfer agent like `mailutils` or `postfix` to be set up). The script runs indefinitely, pausing for 60 seconds between each check, making it suitable for simple uptime monitoring.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs