Sub topic 14
Heartbeat monitoring: alerting when a job does not run
Assume you are comfortable with shell scripts, exit statuses, cron or systemd timers, and reading logs. This chapter focuses on detecting a missing or failed run. A heartbeat does not replace logs. It gives you a fast signal that you need to investigate.
What a heartbeat tells you
A heartbeat is a network request that says, “This job completed successfully.” A monitoring service records the time of the last request. It alerts you when the request is late or when the job reports failure.
For example, a job scheduled every 15 minutes might use a heartbeat check with a 30-minute grace period. A short delay does not alert immediately, but a stopped timer or failed script does.
A useful heartbeat has these properties:
- The job sends it only after successful work.
- The monitor expects it within a defined interval.
- The monitor alerts through a channel such as email, chat, or a paging service.
- The request has a short timeout and limited retries.
- The job's logs remain available for diagnosis.
Send the heartbeat after success
Place the heartbeat command after every operation that must succeed. With set -euo pipefail, a failed command stops the script, so the success request is skipped.
#!/usr/bin/env bash
set -euo pipefail
out=/var/backups/db.sql.gz
tmp=$(mktemp "${out}.XXXXXX")
trap 'rm -f "$tmp"' EXIT
pg_dump mydb | gzip > "$tmp"
mv "$tmp" "$out"
# This runs only after the backup and atomic rename succeed.
curl -fsS -m 10 --retry 3 -o /dev/null \
https://hc.example.com/ping/your-check-idThe -f option makes HTTP errors fail. The -sS options keep normal output quiet while retaining errors. The timeout prevents a network problem from hanging the job indefinitely. Retries handle a temporary connection failure, but they are bounded.
Do not send the heartbeat at the beginning of the script. That would report success even if the main operation fails afterward.
Configure useful alert conditions
Set the expected interval from the schedule, not from the job's average runtime. A job scheduled hourly might need a two-hour grace period if the host can be offline briefly. A critical five-minute job may need a much shorter threshold.
Configure separate alerts for:
- A missed heartbeat.
- An explicit failure notification, if your monitor supports one.
- Repeated failures or unusually long runtime.
- Recovery after the heartbeat resumes.
A missing heartbeat says that success was not confirmed. It does not identify the cause. Check the scheduler, service status, exit status, network access, disk space, and job logs.
Test the complete path
Run the job manually with the same user and environment as the scheduler. Confirm that it creates the expected result and that the monitoring service records the heartbeat. Then make a temporary test copy that fails before the heartbeat. Confirm that the monitor eventually alerts.
For a systemd job, inspect recent output with:
sudo systemctl start backup.service
sudo systemctl status backup.service
journalctl -u backup.service --since todayKeep the monitoring threshold longer than normal startup and network delays, but short enough to give operators time to respond.
Exercise
Where should a successful heartbeat request appear in a backup script, and why?
Show answer
It should appear after every required backup operation succeeds. This prevents a failed or partial backup from being reported as successful.