Website Uptime Monitor Shell Script with Email Alerts

website uptime monitor script

Monitor websites from your Linux server with a simple uptime checker using curl and email alerts.

What this script does

  • Checks multiple URLs for HTTP 200 response
  • Measures response time in milliseconds
  • Sends email alert when site is down
  • Logs all checks with timestamps
  • Run from cron every 5 minutes

Prerequisites

  • curl installed
  • Outbound internet access
  • Optional: mail command

Step 1: Save the script

sudo nano /usr/local/bin/uptime-monitor.sh
sudo chmod +x /usr/local/bin/uptime-monitor.sh

Step 2: Full script (scroll to read)

uptime-monitor.sh
#!/usr/bin/env bash
set -euo pipefail

URLS=("https://blog.gnomeitsolutions.com/" "https://gnomeitsolutions.com/")
TIMEOUT=10
ALERT_EMAIL="[email protected]"
LOG="/var/log/uptime-monitor.log"

log(){ echo "[$(date '+%F %T')] $*" | tee -a "$LOG"; }

check_url(){
  local url="$1"
  local code time
  time=$(curl -o /dev/null -s -w '%{http_code} %{time_total}' --max-time "$TIMEOUT" "$url" || echo "000 0")
  code=$(echo "$time" | awk '{print $1}')
  time=$(echo "$time" | awk '{print $2}')
  if [[ "$code" == "200" || "$code" == "301" || "$code" == "302" ]]; then
    log "OK $url HTTP $code (${time}s)"
  else
    log "DOWN $url HTTP $code"
    [[ -n "$ALERT_EMAIL" ]] && command -v mail >/dev/null && \
      echo "$url returned HTTP $code" | mail -s "DOWN: $url" "$ALERT_EMAIL"
  fi
}

for u in "${URLS[@]}"; do check_url "$u"; done

Scroll inside the box to read the full script.

Step 3: Configure settings

  • URLS — array of sites to monitor
  • TIMEOUT — curl timeout seconds
  • ALERT_EMAIL — alert recipient
Website uptime monitor shell script on Linux
Website uptime monitor shell script on Linux

Step 4: Test manually

sudo /usr/local/bin/uptime-monitor.sh
curl -o /dev/null -s -w '%{http_code}' https://blog.gnomeitsolutions.com/

Schedule with cron

sudo crontab -e

Add:

*/5 * * * * /usr/local/bin/uptime-monitor.sh >> /var/log/uptime-monitor.log 2>&1

Related tutorials

Terminal screenshot is an original illustration created for Gnome IT Solutions (blog.gnomeitsolutions.com).