Sub topic 3
Timeouts and bounded retries
This chapter assumes you can edit a crontab and a systemd unit file, read journalctl output, and write a bash script that uses set -euo pipefail. It also assumes you know the cron and systemd timer syntax from earlier chapters.
A job can hang on a stuck network mount, and a hung job blocks every later run. A job can also fail once because a server is briefly unavailable. Limit both cases: stop a run that takes too long, and retry a fixed number of times.
Timeouts
The timeout command stops a program after a set time by sending it the TERM signal:
timeout 300 /usr/local/bin/sync.shIf the command exceeds 300 seconds, timeout exits with status 124, so the caller can detect it. For systemd services, set TimeoutStartSec=1h in the [Service] section. systemd then kills the job and logs the timeout.
Bounded retries
Retry a fixed number of times, with a longer wait between attempts. An unlimited loop can run forever and hide a permanent failure.
#!/usr/bin/env bash
set -euo pipefail
for attempt in 1 2 3; do
if timeout 300 /usr/local/bin/sync.sh; then
exit 0
fi
echo "attempt $attempt failed" >&2
sleep $((attempt * 30))
done
echo "sync failed after 3 attempts" >&2
exit 1Missed runs
Cron does not run a job that was due while the machine was off. A systemd timer with Persistent=true runs the job once at the next boot, even if several runs were missed. Anacron suits daily or weekly jobs on machines that are often off, because it counts days instead of clock times.
Time zones and daylight saving time
Cron and systemd timers use the server's local time zone. Check it with timedatectl. On daylight saving change days, a job set for 02:30 may run twice or not at all. Schedule jobs away from the 01:00 to 03:00 window, or set servers to UTC.
Least privilege
Run each job as a dedicated user, not root. This service limits what the job can write:
[Service]
Type=oneshot
User=backup
NoNewPrivileges=yes
ProtectSystem=strict
ReadWritePaths=/var/backupsHeartbeat monitoring
A failed job can go unnoticed if nothing checks for its success. A heartbeat service expects a ping after each good run and alerts you when the ping is missing. Ping only after the job succeeds:
curl -fsS -m 10 --retry 3 -o /dev/null https://hc.example.com/ping/your-check-id::: callout note Ping the heartbeat URL only after the last step succeeds. A ping sent earlier hides failures later in the script. :::
Testing and operating
Start the service by hand with sudo systemctl start backup.service, then read the result with journalctl -u backup.service --since today. Confirm the next run with systemctl list-timers. Test a failure by making the script exit with status 1, and check that the retries and the missing heartbeat appear.
Exercise
A job runs timeout 300 sync.sh and the timeout fires. What exit status does the caller see, and why should a script check it?
Show answer
The caller sees status 124. A script should check it so it can log the timeout or retry, instead of treating the run as a generic failure.