Configure a watchdog on Raspberry Pi to monitor if a website/service responds. If the site becomes unresponsive, the Pi will automatically reboot.
Edit boot configuration:
sudo nano /boot/config.txt
Add at the end:
dtparam=watchdog=on
Or for newer Raspberry Pi OS:
sudo nano /boot/firmware/config.txt
sudo nano /etc/modules-load.d/watchdog.conf
Add:
bcm2835_wdt
sudo reboot
After reboot, verify:
lsmod | grep watchdog
# Should show: bcm2835_wdt
ls -l /dev/watchdog*
# Should show: /dev/watchdog and /dev/watchdog0
sudo apt-get update
sudo apt-get install watchdog
sudo nano /etc/watchdog.conf
Configure the following (uncomment and modify):
# Hardware watchdog device
watchdog-device = /dev/watchdog
# Timeout for hardware watchdog (seconds)
watchdog-timeout = 15
# Check interval (seconds)
interval = 10
# Maximum load average (optional)
max-load-1 = 24
max-load-5 = 18
max-load-15 = 12
# Minimum free memory (optional, in bytes)
# min-memory = 1
# Log file
log-dir = /var/log/watchdog
# Realtime priority (optional)
realtime = yes
priority = 1
sudo systemctl enable watchdog
sudo systemctl start watchdog
sudo systemctl status watchdog
sudo nano /usr/local/bin/website-watchdog.sh
Add the following script:
#!/bin/bash
# Configuration
TARGET_URL="http://192.168.1.100:8080" # Change to your website URL
TIMEOUT=10 # Timeout in seconds
MAX_FAILURES=3 # Number of failures before reboot
FAILURE_COUNT_FILE="/tmp/website_watchdog_failures"
LOG_FILE="/var/log/website-watchdog.log"
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Initialize failure count
if [ ! -f "$FAILURE_COUNT_FILE" ]; then
echo 0 > "$FAILURE_COUNT_FILE"
fi
# Read current failure count
FAILURES=$(cat "$FAILURE_COUNT_FILE")
# Check website availability
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time "$TIMEOUT" "$TARGET_URL")
if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 301 ] || [ "$HTTP_CODE" -eq 302 ]; then
# Website is responding - reset failure count
if [ "$FAILURES" -gt 0 ]; then
log_message "SUCCESS: Website $TARGET_URL is back online (HTTP $HTTP_CODE)"
fi
echo 0 > "$FAILURE_COUNT_FILE"
# Pet the watchdog to keep system alive
echo "w" > /dev/watchdog
else
# Website is not responding - increment failure count
FAILURES=$((FAILURES + 1))
echo "$FAILURES" > "$FAILURE_COUNT_FILE"
log_message "FAILURE: Website $TARGET_URL not responding (HTTP $HTTP_CODE) - Attempt $FAILURES/$MAX_FAILURES"
# Check if max failures reached
if [ "$FAILURES" -ge "$MAX_FAILURES" ]; then
log_message "CRITICAL: Max failures reached. Triggering reboot via watchdog..."
# Stop petting the watchdog - system will reboot when timeout expires
exit 1
fi
# Still pet the watchdog if below max failures
echo "w" > /dev/watchdog
fi
exit 0
sudo chmod +x /usr/local/bin/website-watchdog.sh
sudo /usr/local/bin/website-watchdog.sh
Check logs:
tail -f /var/log/website-watchdog.log
sudo nano /etc/systemd/system/website-watchdog.service
Add:
[Unit]
Description=Website Monitoring Watchdog
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/website-watchdog.sh
StandardOutput=journal
StandardError=journal
# Don't restart on failure - let watchdog handle it
Restart=no
[Install]
WantedBy=multi-user.target
sudo nano /etc/systemd/system/website-watchdog.timer
Add:
[Unit]
Description=Website Monitoring Watchdog Timer
Requires=website-watchdog.service
[Timer]
# Run every 30 seconds
OnBootSec=30
OnUnitActiveSec=30
AccuracySec=1s
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable website-watchdog.timer
sudo systemctl start website-watchdog.timer
sudo systemctl status website-watchdog.timer
View logs in real-time:
# Watchdog logs
sudo journalctl -u website-watchdog.service -f
# Custom logs
tail -f /var/log/website-watchdog.log
Stop your web server temporarily:
# Example: if using a systemd service
sudo systemctl stop your-web-service
Watch the logs - after 3 failures (90 seconds with 30s interval), the Pi should reboot.
systemctl list-timers --all | grep website-watchdog
Edit the timer to check more/less frequently:
sudo nano /etc/systemd/system/website-watchdog.timer
Change OnUnitActiveSec=30 to your desired interval (in seconds).
Edit the script:
sudo nano /usr/local/bin/website-watchdog.sh
Change MAX_FAILURES=3 to your desired threshold.
Edit the script and update:
TARGET_URL="http://your-website-here:port"
Modify the script to check multiple URLs:
# Array of URLs to check
URLS=(
"http://192.168.1.100:8080"
"http://localhost:3000"
)
# Check all URLs
ALL_OK=true
for URL in "${URLS[@]}"; do
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time "$TIMEOUT" "$URL")
if [ "$HTTP_CODE" -ne 200 ]; then
ALL_OK=false
log_message "FAILURE: $URL not responding (HTTP $HTTP_CODE)"
fi
done
if [ "$ALL_OK" = true ]; then
echo 0 > "$FAILURE_COUNT_FILE"
echo "w" > /dev/watchdog
else
# Handle failure...
fi
sudo systemctl status website-watchdog.timer
sudo systemctl status website-watchdog.service
sudo systemctl status watchdog
# Systemd journal
sudo journalctl -u website-watchdog.service -n 50
# Custom log file
sudo tail -100 /var/log/website-watchdog.log
# Hardware watchdog logs
sudo journalctl -u watchdog -n 50
last reboot
Check if watchdog device is accessible:
ls -l /dev/watchdog
# Should be: crw------- 1 root root 10, 130
Verify watchdog daemon is running:
sudo systemctl status watchdog
Test watchdog manually:
# This will cause reboot after timeout!
sudo sh -c "echo 'V' > /dev/watchdog"
Check timer status:
systemctl status website-watchdog.timer
systemctl list-timers --all
Check for errors:
sudo journalctl -u website-watchdog.service -n 50 --no-pager
Test curl manually:
curl -v --max-time 10 http://your-website:port
Check network connectivity:
ping -c 4 your-website-ip
/dev/watchdogInstall mail utilities:
sudo apt-get install mailutils ssmtp
Configure SMTP in /etc/ssmtp/ssmtp.conf, then add to script:
log_message "CRITICAL: Sending notification email..."
echo "Website $TARGET_URL failed $FAILURES times. Rebooting..." | \
mail -s "Watchdog: Raspberry Pi Reboot Triggered" admin@example.com
sudo systemctl stop website-watchdog.timer
sudo systemctl disable website-watchdog.timer
sudo systemctl stop watchdog
sudo systemctl disable watchdog
✅ Hardware watchdog enabled in /boot/config.txt
✅ Watchdog daemon installed and configured
✅ Monitoring script checks website every 30 seconds
✅ After 3 consecutive failures (90 seconds), reboot is triggered
✅ Logs available in /var/log/website-watchdog.log and systemd journal
The system is now self-healing and will automatically reboot if the monitored website becomes unresponsive.