Skip to content

Monitor Linux Cron Jobs

Cron runs your job and tells nobody when it fails or stops running. Add a curl call to the crontab line, and Chronos alerts you when the job fails or misses its schedule.

Create a monitor with the same cron expression and timezone as the crontab line, then copy its ping URL.

Terminal window
# m h dom mon dow command
0 3 * * * /usr/local/bin/backup.sh && curl -fsS -m 10 --retry 5 -o /dev/null https://ping.chronos.sh/k3Jx9Qm2TnV7bRw4YcZ8dHfL

&& runs curl only when backup.sh exits with 0. When the job fails, no ping arrives, and the monitor goes down once the grace period passes.

Flag What it does
-f Exits with an error on an HTTP status of 400 or above, so a wrong URL shows up in cron’s mail.
-s Hides the progress meter.
-S Shows errors even with -s.
-m 10 Gives up on a request after 10 seconds, so a network problem can’t hang the job.
--retry 5 Retries up to 5 times on a timeout or a transient HTTP error, such as 429 or 503.
-o /dev/null Discards the OK response body.

To get the down alert when the job fails, instead of after the grace period, send the exit code:

Terminal window
0 3 * * * /usr/local/bin/backup.sh; curl -fsS -m 10 --retry 5 -o /dev/null https://ping.chronos.sh/k3Jx9Qm2TnV7bRw4YcZ8dHfL/$?

; runs curl whatever the job’s result, and $? is the job’s exit code. Chronos treats /0 as a success and any other code from 1 to 255 as a failure, and the down alert shows the code.

Send a start ping before the job runs:

Terminal window
0 3 * * * curl -fsS -m 10 --retry 5 -o /dev/null https://ping.chronos.sh/k3Jx9Qm2TnV7bRw4YcZ8dHfL/start; /usr/local/bin/backup.sh; curl -fsS -m 10 --retry 5 -o /dev/null https://ping.chronos.sh/k3Jx9Qm2TnV7bRw4YcZ8dHfL/$?

The ping history then shows each run’s duration. A start ping never moves the deadline, so the grace period still has to cover the whole run.

Once the line gets long, move it into a wrapper script. This one also sends the last 500 bytes of the job’s output, since the down alert shows the first 500 characters of a fail ping’s body:

/usr/local/bin/backup-with-ping.sh
#!/bin/sh
url=https://ping.chronos.sh/k3Jx9Qm2TnV7bRw4YcZ8dHfL
curl -fsS -m 10 --retry 5 -o /dev/null "$url/start"
output=$(/usr/local/bin/backup.sh 2>&1)
code=$?
printf '%s' "$output" | tail -c 500 | curl -fsS -m 10 --retry 5 -o /dev/null --data-binary @- "$url/$code"

Save it as /usr/local/bin/backup-with-ping.sh, make it executable, and point the crontab line at it:

Terminal window
0 3 * * * /usr/local/bin/backup-with-ping.sh

Run the line once by hand and look at the monitor in the dashboard. Its status changes from new to up, and the ping shows in its history. To test the alert, run the /$? line with a command that fails, such as false, in place of backup.sh.