SSL certificate expiry and API uptime check script

Updated December 2, 2026

You don't need a full monitoring SaaS to catch a dead health endpoint or a forgotten Let's Encrypt renewal. A short script using only curl and openssl (or a free Cloudflare Worker cron) checks your sites in seconds and pings your phone only when something breaks or a certificate is within 14 days of expiry.

Create a send key in NextMsg (Settings → Send keys → Add send key → Copy a key) and store it in NEXTMSG_KEY.

1. Bash script (curl + openssl, zero extra packages)

Save this as /usr/local/bin/check-ssl-uptime.sh and chmod +x it. It checks HTTP status codes and TLS expiry dates, alerts immediately on failure, and tracks state in /tmp/uptime-down-* so a 30-minute outage sends one down alert and one recovery alert instead of repeating every 5 minutes:

#!/usr/bin/env bash
set -euo pipefail

URLS=(
  "https://example.com/health"
  "https://api.example.com/status"
)
DOMAINS=(
  "example.com"
  "api.example.com"
)
WARN_DAYS=14
MODE="${1:-check}" # pass "daily" once a day to check certs & send summary

notify() {
  curl -fsS -X POST "https://api.nextmsg.app/v1/send?channel=$1" \
    -H "Authorization: Bearer $NEXTMSG_KEY" \
    --data-raw "$2" >/dev/null
}

# 1. HTTP uptime checks with down/recovery deduplication
for url in "${URLS[@]}"; do
  slug=$(printf '%s' "$url" | tr -c 'a-zA-Z0-9' '_')
  flag="/tmp/uptime-down-${slug}"
  code=$(curl -sS -o /dev/null -w "%{http_code}" --max-time 10 "$url" || echo "000")

  if [[ "$code" -lt 200 || "$code" -ge 400 ]]; then
    if [[ ! -f "$flag" ]]; then
      touch "$flag"
      notify "uptime" "**DOWN** \`$url\` (HTTP $code)"
    fi
  elif [[ -f "$flag" ]]; then
    rm -f "$flag"
    notify "uptime" "**RECOVERED** \`$url\` (HTTP $code)"
  fi
done

# 2. TLS certificate expiry check (run once a day with: check-ssl-uptime.sh daily)
if [[ "$MODE" == "daily" ]]; then
  expiring=""
  min_days=9999
  for host in "${DOMAINS[@]}"; do
    end_date=$(echo | openssl s_client -servername "$host" -connect "$host:443" 2>/dev/null \
      | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2)
    [[ -z "$end_date" ]] && expiring+="${host} (unreachable)"$'\n' && continue
    end_epoch=$(date -d "$end_date" +%s 2>/dev/null || date -j -f "%b %e %T %Y %Z" "$end_date" +%s)
    days=$(( (end_epoch - $(date +%s)) / 86400 ))
    (( days < min_days )) && min_days=$days
    (( days <= WARN_DAYS )) && expiring+="${host}: **${days}d left**"$'\n'
  done

  if [[ -n "$expiring" ]]; then
    notify "uptime" "**SSL cert expiring soon**"$'\n'"$expiring"
  else
    notify "stats" "**Uptime OK** · ${#URLS[@]} endpoints healthy · soonest cert **${min_days}d**"
  fi
fi

2. Schedule with cron

Run the HTTP check every 5 minutes and the TLS expiry + morning heartbeat once a day at 08:00. Always run this from a separate VPS or machine than the servers you are monitoring:

NEXTMSG_KEY=nextmsg_live_your_key_here

*/5 * * * * /usr/local/bin/check-ssl-uptime.sh
0 8 * * *   /usr/local/bin/check-ssl-uptime.sh daily

3. Serverless option: Cloudflare Worker cron

If you don't want to maintain a separate VPS just for uptime checks, a free Cloudflare Worker runs on a 5-minute cron from Cloudflare's network even when your laptop is closed:

const ENDPOINTS = [
  "https://example.com/health",
  "https://api.example.com/status"
];

export default {
  async scheduled(_controller: ScheduledController, env: { NEXTMSG_KEY: string }) {
    const failures: string[] = [];

    for (const url of ENDPOINTS) {
      try {
        const res = await fetch(url, { signal: AbortSignal.timeout(8000) });
        if (!res.ok) failures.push(`${url} (HTTP ${res.status})`);
      } catch (err) {
        failures.push(`${url} (${err instanceof Error ? err.message : "timeout"})`);
      }
    }

    if (failures.length === 0) return;

    await fetch("https://api.nextmsg.app/v1/send", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${env.NEXTMSG_KEY}`,
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        channel: "uptime",
        message: `**Endpoint check failed**\n${failures.join("\n")}`.slice(0, 640)
      })
    });
  }
};

When to use a dedicated uptime tool instead

  • Keep the script when you have 2–10 endpoints, want zero extra accounts, and already run a small VPS or Cloudflare Worker.
  • Use Uptime Kuma, Phare, or Healthchecks.io when you want status pages, multi-region retries, response-time graphs, or dead-man switches for cron jobs — all three connect to NextMsg in under two minutes.

More guides