#!/bin/bash # Configuration URL="https://your-website.com" EXPECTED_STATUS="200" RECIPIENT_EMAIL="admin@your-website.com" LOG_FILE="/var/log/website_health.log" # Function to send email alert send_alert() { SUBJECT="Website Down Alert: $URL" BODY="Website $URL is returning HTTP status $1 instead of $EXPECTED_STATUS at $(date)." echo -e "$BODY" | mail -s "$SUBJECT" "$RECIPIENT_EMAIL" echo "$(date) - ALERT SENT: $BODY" >> "$LOG_FILE" } # Check website status HTTP_STATUS=$(curl -o /dev/null -s -w '%{http_code}' "$URL") if [ "$HTTP_STATUS" -ne "$EXPECTED_STATUS" ]; then send_alert "$HTTP_STATUS" echo "$(date) - Website $URL is DOWN (Status: $HTTP_STATUS)" >> "$LOG_FILE" else echo "$(date) - Website $URL is UP (Status: $HTTP_STATUS)" >> "$LOG_FILE" fi