Cron has been scheduling Linux tasks for decades, yet its default failure behavior is still stuck in 1985: it tries to pipe output to a local mail daemon (sendmail or postfix) that almost no modern cloud VM or container actually configures.

The result? Your nightly PostgreSQL backup, SSL renewal script, or billing sync silently crashes, and you only discover it when something downstream breaks. Here is how to fix that permanently without installing heavy monitoring agents.

1. The 10-Second Crontab One-Liner

If you want immediate coverage without touching your existing scripts, chain a curl request behind the POSIX || (OR) operator. When your job exits with 0, nothing fires. The moment it returns any non-zero error code, AlertHub dispatches your alert.

crontab
# Run nightly at 02:00 UTC and alert only on failure
0 2 * * * /usr/local/bin/backup.sh || curl -fsS -m 5 -X POST https://alerthub.io/api/event/YOUR_USER_ID \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"title":"cron_failure","body":"Nightly backup failed on prod-db-1"}' > /dev/null 2>&1
Why -m 5 matters: Always pass a max-time flag (5 seconds) to curl inside cron jobs so transient network hiccups never leave hung processes piling up on your server.

2. Capture stderr & Exit Codes with a Wrapper Script

Knowing a job failed is good, but reading the exact stderr stack trace right on your phone lock screen or Slack channel saves you from SSHing into production at 3:00 AM.

Drop this reusable wrapper into /usr/local/bin/alert-on-fail. It executes any command, captures combined output, safely JSON-escapes special characters, and attaches the last 1,500 characters of logs to your AlertHub event.

/usr/local/bin/alert-on-fail
#!/usr/bin/env bash
export PATH="/usr/local/bin:/usr/bin:/bin"

ALERTHUB_USER_ID="${ALERTHUB_USER_ID:-YOUR_USER_ID}"
ALERTHUB_API_KEY="${ALERTHUB_API_KEY:-YOUR_API_KEY}"
JOB_NAME="${1:?Job name required}"
shift

# Run the command and capture combined stdout + stderr
if OUTPUT=$("$@" 2>&1); then
    exit 0
else
    EXIT_CODE=$?
fi

# Safely serialize the payload (compatible with Python 3.6+ on older Linux distros)
JSON_PAYLOAD=$(JOB_NAME="$JOB_NAME" EXIT_CODE="$EXIT_CODE" python3 -c '
import json, os, socket, sys

job = os.environ["JOB_NAME"]
code = os.environ["EXIT_CODE"]
host = socket.gethostname()
logs = sys.stdin.read().strip()[-1500:] or "(no output)"

body = f"Host: {host}\nExit Code: {code}\n\nOutput:\n{logs}"

print(json.dumps({
    "title": f"Cron failed: {job}",
    "body": body,
    "status": "error",
    "priority": "high",
    "source": "cron"
}))
' <<< "$OUTPUT")

curl -fsS -m 5 -X POST "https://alerthub.io/api/event/${ALERTHUB_USER_ID}" \
  -H "X-API-KEY: ${ALERTHUB_API_KEY}" \
  -H "Content-Type: application/json" \
  -d "$JSON_PAYLOAD" > /dev/null 2>&1

exit "$EXIT_CODE"

3. Three Cron Gotchas That Break Alerts

If your wrapper works when tested manually over SSH but fails when triggered by cron, check these three classic traps:

  • Minimal $PATH: Cron defaults to /usr/bin:/bin. Tools in /usr/local/bin (like Docker or Go binaries) will fail unless PATH is exported explicitly.
  • Unescaped % signs: Cron interprets unescaped percent symbols in crontab entries as newlines. Always escape them (\%) or keep logic inside a script file.
  • Assuming .bashrc loads: Non-interactive shells skip interactive profile exports. Load secrets from a dedicated environment file instead.

4. Route Alerts Across All Your Channels

Because AlertHub decouples event ingestion from delivery routing, your server only ever makes one lightweight HTTP request. From your AlertHub dashboard, you can create delivery profiles across Mobile Push, Slack, Email, Telegram, WhatsApp, or upstream webhooks without ever editing your server's crontab again.