Monitor systemd timers with a heartbeat
A systemd timer that stops firing, or a service that fails every night, shows up in systemctl list-timers and the journal, and nowhere else. A Tissue Mast heartbeat expects a beat after each run and pages you when one is missing or reports a failure.
Keep the heartbeat URL in an EnvironmentFile=. For a beat on success, add ExecStartPost= to the oneshot service. To report failures as well, send the exit status from ExecStopPost=, which runs however the service ended.
The timer and the service
# /etc/systemd/system/backup.timer [Unit] Description=Nightly backup [Timer] OnCalendar=*-*-* 03:17:00 America/Los_Angeles Persistent=true [Install] WantedBy=timers.target
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup
[Service]
Type=oneshot
EnvironmentFile=/etc/backup.env
ExecStart=/usr/local/bin/backup.sh
ExecStartPost=-/usr/bin/curl -fsS -m 10 --retry 3 -X POST ${HEARTBEAT_URL}
# /etc/backup.env (chmod 600) HEARTBEAT_URL=https://mast.tissue.dev/mk_8e2a…
systemctl daemon-reload systemctl enable --now backup.timer systemctl start backup.service # one run by hand, to see the first beat
Type=oneshot- For a oneshot service, systemd runs
ExecStartPost=only after theExecStart=process has exited successfully. A failed backup never reaches the curl, and the heartbeat goes down when its grace runs out. ${HEARTBEAT_URL}- systemd fills it in from
EnvironmentFile=, so the key stays out of the unit file and out of anything you commit. A variable can't be the first word of the command, which is why curl is named in full. - The
-before/usr/bin/curl - A failure of the curl itself is recorded and otherwise treated as success, so a network hiccup doesn't mark a good backup as failed. Without it, a failing
ExecStartPost=puts the unit in the failed state. Persistent=true- If the machine was off at 03:17, the service runs as soon as the timer starts again. The beat then arrives late rather than never.
Report failures with ExecStopPost=
To hear about a failed run the moment it fails, report from ExecStopPost= instead. systemd runs it however the service ended and passes it $SERVICE_RESULT, $EXIT_CODE and $EXIT_STATUS. A short script turns those into a Mast path:
#!/bin/sh
# /usr/local/bin/mast-report
# Run from ExecStopPost=. Sends /0 for a clean run, /N for exit code N,
# and /fail for a timeout or a signal.
case "$SERVICE_RESULT" in
success) path=0 ;;
*)
case "$EXIT_CODE:$EXIT_STATUS" in
exited:[1-9]*) path=$EXIT_STATUS ;;
*) path=fail ;;
esac ;;
esac
exec curl -fsS -m 10 --retry 3 -X POST "$HEARTBEAT_URL/$path"
# /etc/systemd/system/backup.service [Unit] Description=Nightly backup [Service] Type=oneshot EnvironmentFile=/etc/backup.env ExecStart=/usr/local/bin/backup.sh ExecStopPost=-/usr/local/bin/mast-report
$EXIT_STATUS holds the numeric exit code when $EXIT_CODE is exited, and the signal name otherwise, which is why a signal goes to /fail. The heartbeat sees:
| Path | What happens |
|---|---|
/0 | A beat. The heartbeat stays up. |
/2 | Down straight away, and the card reads backup exited 2. |
/fail | Down straight away, with no code to name. |
On systemd 249 and later, OnSuccess= in the [Unit] section can start a separate unit when the service finishes cleanly, next to the older OnFailure=. That works too, but it's two more units to keep for what ExecStopPost= does in one line.
The heartbeat to go with it
Give the heartbeat the same time as the timer. Mast takes a crontab line and an IANA zone, so *-*-* 03:17:00 America/Los_Angeles becomes:
curl -X PATCH https://api.tissue.systems/v1/mast/alert-sources/backup \
-H "Authorization: Bearer $TISSUE_TOKEN" -H "Content-Type: application/json" \
-d '{"schedule": "17 3 * * *", "schedule_tz": "America/Los_Angeles", "grace_secs": 1800}'
Or set it in the web app under Heartbeats. The grace is how long after 03:17 the beat may arrive before anyone is woken, so give it the backup's real run time plus room. A timer that fires every 15 minutes is simpler as a period: period_secs of 900 and a grace of a few periods.
To catch a run that starts and never finishes, add ExecStartPre=-/usr/bin/curl -fsS -m 10 --retry 3 -X POST ${HEARTBEAT_URL}/start and set a longest run on the heartbeat. Run signals explains how the two fit together.
Free is ten heartbeats with an email address and no credit card. Team is $29 a month for 250 heartbeats, with rotations and escalation for everyone on call.