Keep total retry time shorter than the schedule interval

A job that retries for longer than its own schedule creates overlap risk even when a lock is present. The lock prevents simultaneous execution, yet a job that keeps colliding with its own lock eventually stops making forward progress. Calculate the maximum backoff window before writing the retry loop and test it against the shortest interval you expect to use.

For a job scheduled every five minutes, a total retry window of two minutes leaves headroom. For nightly jobs the window can be longer, but still keep it well under twenty-four hours so the next scheduled run is not blocked.

  • Use exponential backoff with a cap rather than fixed delays.
  • Stop retrying on non-transient errors such as authentication failures or missing configuration files.
  • Log each retry attempt with the error that triggered it so later review shows whether the backoff was effective.

Make the job idempotent before adding retries

Retries only help when repeating the same work produces the same final state. Garbage collection tasks are naturally idempotent. Newsletter delivery or payment processing are not. Before enabling retries, add an explicit check that records the last successful run identifier or timestamp in a durable store.

When the job starts, read the last recorded identifier. If the current run matches a previously completed identifier, exit cleanly without repeating side effects. This check must itself be atomic so two instances cannot both decide they are the first to run.

  • Store the identifier in a database row or key-value entry with a TTL that matches your longest expected retry window.
  • Use an upsert or conditional write so the check and record happen in one operation.
  • Test the idempotency guard by deliberately running the job twice in quick succession.

Add a dead man's switch after the work succeeds

The most expensive failure is a job that stops running at all. A deleted crontab, a rebuilt container image, or an auto-disabled GitHub Actions schedule produces no error output, so any alert that waits for a non-zero exit code never fires. A dead man's switch solves this by requiring the job to actively check in when it finishes.

The check-in must occur only after the main work and any retries have completed successfully. Placing the ping inside a shell trap or finally block causes it to fire on every exit, including failures, which defeats the purpose. Run the ping as the final statement in the script so the monitor only receives a signal on clean completion.

  • Hosted services such as Healthchecks.io or Cronitor provide simple HTTPS endpoints that record the check-in time.
  • Self-hosted alternatives include a Prometheus Pushgateway or a small database row that an external alert queries on a schedule.
  • Pair the success ping with two additional alerts: one for non-zero exit codes and one for runtime that exceeds a threshold based on historical measurements.

Log start, end, and duration for every run

Cron itself only records that it attempted to start a job. Without deliberate logging, output disappears and later investigation has no timeline. Begin every script with strict shell options so errors stop execution and unset variables cause immediate failure rather than silent substitution.

Write a start line, then the main work, then an end line that includes the exit code and elapsed seconds. Rotate the log file with logrotate so it does not grow indefinitely. These two echo statements answer the question of whether the most recent scheduled run actually occurred and how long it took.

  • Use set -euo pipefail at the top of every Bash script used by cron.
  • Append both stdout and stderr to a single file with timestamps so the sequence of events is preserved.
  • Add a logrotate rule that keeps fourteen days of compressed logs and uses copytruncate to avoid losing entries during rotation.

Test the full failure path before relying on it

A retry loop or dead man's switch that has never been exercised in production will fail when it is needed most. Schedule a test run that deliberately triggers a transient error, confirm the retry occurs, and verify the success ping is withheld until the final successful attempt. Repeat the test after any change to the script or the monitoring endpoint.

Document the expected behavior for each failure mode so future maintainers know whether a missing ping means the job is broken or simply that the schedule has not yet arrived.

  • Simulate network failure by temporarily blocking the destination host with iptables or a container network policy.
  • Verify that the monitor alerts within one schedule interval after the success ping stops arriving.
  • Keep the test procedure in a runbook so it can be repeated after infrastructure changes.