Monitor Linux crontab jobs with curl

On this page

Classic cron has no idea whether a job worked: it starts the command, mails the output to a mailbox nobody reads, and forgets. Add one curl to the end of the crontab line and you get an alert the first time the ping does not arrive.

How can a scheduled job fail silently on Linux crontab?

  • cron mails a job's output to the owner of the crontab, or to the address in MAILTO. If MAILTO is set to an empty string, no mail is sent at all, and on many servers there is no mail system to receive it.
  • Jobs start with a small environment: SHELL is /bin/sh, and LOGNAME and HOME come from the owner's passwd entry. Commands that work in your shell can fail under cron for lack of a PATH entry or a variable.
  • A percent sign in the command part of a line is turned into a newline and everything after the first one is sent to the command as input, so a date format like +%Y-%m-%d silently breaks a job unless each % is escaped.
  • cron(8) handles local time changes of under three hours in a special way: jobs set to a fixed time run once or are moved, and jobs that run more often than hourly are scheduled normally.
  • The daemon reloads crontab files when their modification time changes, but a crontab that has a syntax error is rejected, and its jobs never run.

What are the limits of Linux crontab?

LimitWhat the documentation says
FieldsFive: minute, hour, day of month, month, day of week. Names for months and weekdays are accepted, and Sunday is 0 or 7.
Day fields togetherIf both day-of-month and day-of-week are restricted, the job runs when either matches.
EnvironmentSHELL=/bin/sh; LOGNAME and HOME from the passwd line; HOME and SHELL can be set in the crontab.
MAILTOUnset: mail goes to the crontab owner. Empty: no mail. A value: mail goes to that address.
Percent signTreated as a newline in the command unless escaped with a backslash.

How do you add a CronMissed ping to Linux crontab?

Run the job and ping only when it succeeds, or report the exit status either way. The first line pings on success; the second reports every exit status, so a failure turns the check down at once.

crontab
15 3 * * * /usr/local/bin/backup.sh && curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null "https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10"
crontab
15 3 * * * /usr/local/bin/backup.sh; curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null "https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10/$?"

Which check settings fit Linux crontab?

Create a cron check with the same five fields and the time zone of the server. Set the grace to the run time of the job plus a couple of minutes; a backup that normally takes 10 minutes can have 15 minutes of grace. If the server's clock follows daylight saving, read the cron page for your expression to see what happens on the two clock-change nights.

How do you confirm the check works?

  1. Run the command by hand with the same environment: env -i /bin/sh -c "your command" shows what cron sees.
  2. Wait for one real run, or temporarily set the schedule to every minute, and confirm the ping in the dashboard.
  3. Rename the script so the command fails and confirm the check does not receive the success ping.

Sources

Questions and answers

Why use && and not a semicolon?
With && the ping is sent only when the job exits 0, so a failed job sends nothing and the check goes down once the grace runs out. A semicolon sends the ping whatever happened, which hides failures.
What if the job produces a lot of output?
Send only the end of it: pipe the output through tail -c 100000 into curl --data-binary @-. A ping keeps up to 100 kB of its body, and the end is where the error usually is.
Know when this job stops

Create a check, add the ping to the job, and CronMissed alerts you the first time a run is late or reports a failure.

Start monitoring