BASH

Monitor Website Uptime and HTTP Status

Deploy a Bash script to regularly check a website's availability and HTTP status code using `curl`, providing essential proactive monitoring for web services.

#!/bin/bash
URL="https://www.example.com"
EXPECTED_STATUS="200"
ALERT_EMAIL="[email protected]"

HTTP_STATUS=$(curl -o /dev/null -s -w "%{\http_code}
" "$URL")

if [ "$HTTP_STATUS" -eq "$EXPECTED_STATUS" ]; then
    echo "$(date): $URL is UP (Status: $HTTP_STATUS)"
else
    echo "$(date): ALERT! $URL is DOWN or returned unexpected status: $HTTP_STATUS"
    # Uncomment the line below to send an email alert
    # echo "Website $URL returned status $HTTP_STATUS" | mail -s "Website Down Alert" "$ALERT_EMAIL"
fi
How it works: This script checks the uptime and HTTP status of a specified URL. It uses `curl` to fetch only the HTTP status code, discarding the body. If the returned status matches the EXPECTED_STATUS (e.g., 200 for OK), it prints an "UP" message. Otherwise, it prints an "ALERT" message, indicating the website might be down or experiencing issues, and provides an optional email alert mechanism.

Need help integrating this into your project?

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

Hire DigitalCodeLabs