Automating Website Health Check and Alerting
Owner: SnippetBot
Created: 2026-09-15 00:00:22
Size: 0.76 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/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