Sub topic 11
Alerting on Failure and on Silence
This chapter assumes you have read the earlier chapters on exit codes and logging. You should have the wrapper script that applies timeout, writes a timestamped log, and exits with the agent's status. Here you will make that status reach a person.
Two ways a job goes wrong
A scheduled agent job can fail in two ways.
- Loud failure: the job runs and exits non-zero. The cause might be a timeout (status 124), a crash, or an error from the agent.
- Silent failure: the job never runs, or never finishes. The cause might be a stopped cron daemon, a disabled timer, an expired CI schedule, or a powered-off host.
An exit code can't report a job that never started. You need one check for each kind of failure.
Alerting on a non-zero exit
Alert from a thin outer script. Alerting inside the agent's own script is risky, because a crash or a timeout kill could stop the alert from being sent. The script below runs the nightly wrapper, then reports the result.
#!/usr/bin/env bash
# run-with-alerts.sh
set -u
: "${ALERT_WEBHOOK_URL:?set ALERT_WEBHOOK_URL}"
: "${HEARTBEAT_URL:?set HEARTBEAT_URL}"
/home/deploy/bin/nightly-agent.sh
status=$?
if [ "$status" -ne 0 ]; then
curl -fsS -m 10 -X POST -H 'Content-Type: application/json' \
-d "{\"text\": \"nightly-agent failed with status $status on $(hostname)\"}" \
"$ALERT_WEBHOOK_URL" || echo "alert delivery failed" >&2
else
curl -fsS -m 10 "$HEARTBEAT_URL" > /dev/null || echo "heartbeat failed" >&2
fi
exit "$status"Keep the alert short. Include the status, the host, and the log path so the reader can start debugging at once. Never paste the log contents into the message, because logs can contain secrets or untrusted text.
Alerting on silence
A heartbeat is a URL that the job requests after each success. A monitoring service, or your own endpoint, raises an alarm when no request arrives within the expected window. This is often called a dead-man's switch. Set the window longer than the schedule interval, for example 26 hours for a daily job.
If you have no external service, run a freshness check from a different scheduler or machine.
#!/usr/bin/env bash
# check-fresh.sh: run hourly from a separate scheduler
set -u
LOG_DIR="$HOME/agent-logs"
newest=$(find "$LOG_DIR" -name 'nightly-*.log' -mmin -1560 | head -n 1)
if [ -z "$newest" ]; then
echo "no nightly log in the last 26 hours" >&2
exit 1
fiA fresh log only proves the job started. The heartbeat is stronger, because it fires only after a successful exit.
Common errors
- The checker shares the failure. If the watcher runs on the same host and scheduler as the job, one outage silences both. Put it somewhere independent.
- Alert fatigue. Alerting on every retry trains people to ignore the channel. Alert after the final failure.
- Untested alerts. Break the job on purpose once, for example with
AGENT_CMD=false, and confirm that a message arrives. - Secrets in the webhook URL. Treat it as a credential. Load it from the environment file, not from the script.
Exercise
Your nightly job's host loses power on Tuesday night. No log is written and no exit code is produced. Which mechanism detects this, and why does the failure-alert code not?
Show answer
The heartbeat or freshness check detects it. The failure alert only runs when the wrapper runs and exits non-zero. If the host is off, nothing runs, so only an independent watcher that notices the missing heartbeat or stale log can raise the alarm.