Monitor a WordPress site with a shell script and cron
An outage that nobody notices for six hours costs more than the outage itself. Before paying for a monitoring service, a small server can watch itself: a shell script checks that the site answers quickly, that the disk is not full and that the PHP error log is not exploding, and sends an email only when something changes.
Problem: The site was down overnight because the disk was full, and the client noticed it first.
Solution: Write a Bash script that measures the HTTP status and response time with curl, the disk usage with df and the size of the error log, keep a state file so an alert is sent once per problem (and once when it is resolved), and run it every five minutes from cron.
The script
#!/usr/bin/env bash
# /usr/local/bin/ha-health.sh
set -uo pipefail
SITE="https://example.com/"
MAIL_TO="admin@example.com"
MAX_TIME=3 # seconds until the first byte
MAX_DISK=90 # percent
LOG="/var/www/example/wp-content/debug.log"
MAX_LOG_MB=100
STATE="/var/lib/ha-health/state"
mkdir -p "$(dirname "$STATE")"
problems=()
read -r code ttfb < <(curl -s -o /dev/null -w '%{http_code} %{time_starttransfer}\n' \
--max-time 15 -A 'ha-health/1.0' "$SITE" || echo "000 15")
[ "$code" = "200" ] || problems+=("HTTP status $code")
awk -v t="$ttfb" -v m="$MAX_TIME" 'BEGIN { exit !(t > m) }' && problems+=("slow: ${ttfb}s to first byte")
disk=$(df --output=pcent / | tail -n 1 | tr -dc '0-9')
[ "$disk" -ge "$MAX_DISK" ] && problems+=("disk ${disk}% full")
if [ -f "$LOG" ]; then
log_mb=$(( $(stat -c %s "$LOG") / 1024 / 1024 ))
[ "$log_mb" -ge "$MAX_LOG_MB" ] && problems+=("debug.log is ${log_mb} MB")
fi
now=$(printf '%s\n' "${problems[@]:-}" | sed '/^$/d')
before=$(cat "$STATE" 2>/dev/null || true)
if [ "$now" != "$before" ]; then
if [ -n "$now" ]; then
printf '%s\n' "$now" | mail -s "[ALERT] $(hostname): $SITE" "$MAIL_TO"
else
echo "All checks pass again." | mail -s "[OK] $(hostname): $SITE" "$MAIL_TO"
fi
printf '%s' "$now" > "$STATE"
fi
time_starttransfer is the time until the first byte of the answer, the number that shows a slow PHP or database. The state file holds the current list of problems; mail goes out only when the list changes, so a problem that lasts two hours sends one alert, not 24. The mail command comes with the mailutils package and needs a working mail setup on the server (a relay through your SMTP provider).
Run it from cron
sudo chmod +x /usr/local/bin/ha-health.sh sudo crontab -e # */5 * * * * /usr/local/bin/ha-health.sh
How cron works and how to keep jobs from overlapping: Linux cron jobs for WordPress.
What a script on the same server cannot see
If the whole server is down, the script is down with it. Run the HTTP check also from a second machine (another VPS, a CI scheduled job) or use a free external uptime check for that case, and keep the local script for the things only the server sees: disk, logs, services.
systemctl is-active --quiet mysql || echo "MySQL is not running" systemctl is-active --quiet php8.1-fpm || echo "PHP-FPM is not running"
These two lines fit into the same script as further checks.
NOTE: A large debug.log in wp-content is also a security problem when it is reachable from the browser: it can contain paths and SQL queries. Log outside the web root with define( 'WP_DEBUG_LOG', '/var/log/wordpress/debug.log' );. For finding the cause of a slow server: htop, vmstat and iostat.