Cron job monitoring

Cron writes one line when it starts a job:

CRON[18571]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)

That line is enough to know the job ran on time. When it's missing, the job didn't.

Free 14-Day Trial
**No credit card required.

What cron logs, and where

By default cron logs one line per job, when it starts it, with the user and the command. Two real lines from one of our servers, at midnight:

Dec  2 00:00:01 web01 CRON[3706]: pam_unix(cron:session): session opened for user root by (uid=0)Dec  2 00:00:01 web01 CRON[3707]: (root) CMD (test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew)

The CMD line is the one that matters. The pam_unix line goes to the auth log on most systems, and it only says a cron session opened for root.

Where cron's log lines end up
System Where
Debian, Ubuntu with rsyslog /var/log/syslog
RHEL, Rocky, Alma /var/log/cron
Only journald journalctl -u cron (Debian, Ubuntu) or journalctl -u crond (RHEL)

Debian and Ubuntu cron can log more with -L. Add the levels up: -L 15 logs everything.

Debian and Ubuntu cron log levels
Level Logs
1 The start of each job (the default)
2 The end of each job
4 Jobs that failed
8 The PID of each job

Check man cron on your release for where to set it.

By hand, on one server

For one job on one server it's a one-liner. When did cron last start cron.hourly:

grep 'CMD (cd / && run-parts --report /etc/cron.hourly)' /var/log/syslog | tail -1

On RHEL, use /var/log/cron. With only journald:

journalctl -u cron --since -2h | grep cron.hourly

And you can turn that into a check that complains when the last run is too old:

#!/bin/sh# complain if cron.hourly hasn't started in the last 70 minutesn=$(journalctl -u cron --since -70min -o cat | grep -c cron.hourly)[ "$n" -eq 0 ] && echo "cron.hourly missing on $(hostname)" | mail -s "cron check" ops@example.com

Now do that for every job on every server, and keep the list current when someone adds one. Also, a check like this runs from cron. When cron stops, the check stops with it (go figure). So it has to run from another machine, which means shipping the logs there first.

Ping monitors

The common fix is a dead man's switch: the job calls a URL when it runs, and a service alerts when the call doesn't come. Healthchecks.io gives each check a ping URL; when no ping arrives within the period plus a grace time, it alerts. It understands cron expressions, and the job can also report /start and /fail. Cronitor does the same with run, complete and fail events from the job, and can alert on how long a run took.

0 2 * * * /usr/local/bin/backup.sh && curl -fsS https://hc-ping.com/your-uuid

They're good at this. With && (or a /fail call), they know the job finished and succeeded, which a log line can't tell you. For the backup and the certificate renewal, use one.

The cost of the ping model is that every job needs the call added, and somebody has to remember it for the next job. The jobs that come with packages (certbot, logrotate, sessionclean) rarely get one, and those are the ones nobody looks at. Cronitor also has integrations that find existing jobs on a server.

Learning from the logs

If cron's lines already reach a central log server, the schedule is in them. Look at a week of CMD lines and it falls out: cron.hourly at :17, sysstat every 10 minutes, a backup daily at 02:00.

That's what Schedules in Trunc does. Each job's hours have to show up on 5 different days (about a week) before its schedule is confirmed. Then it alerts when a run is late: after 3 missed runs for a job that runs every few minutes, about 15 minutes for an hourly one, 60 minutes for a daily one, 3 hours for a weekly one. One alert per job per outage.

When we stopped cron on one of our servers for 19 hours, cron.hourly was due at 23:17 UTC and the alert went out at 23:34. It uses the account's time zone, Los Angeles:

root's cron job did not show up on web01 at 15:17 PST:  cd / && run-parts --report /etc/cron.hourlyIt runs every hour at :17.Last seen: 14:17 PST (1 h 17 min ago)Check crontab -l -u root and that cron is running.

It learns SSH logins, sudo and su that happen on a schedule the same way. The full rules are on how Trunc learns schedules, and all six alerts from the test are in we stopped cron for 19 hours.

Which one to use

What each way of monitoring cron covers
What you need By hand Ping monitor From your logs
Setup per job Yes, a check per job Yes, a ping per job No, learned from CMD lines
Jobs nobody wired up No No, except Cronitor's job discovery Yes, if cron's lines arrive
Knows it succeeded Yes, if your script checks Yes, with && or /fail No, only that it started
Cron itself stopped No, if the check uses cron Yes, the pings stop Yes, one alert per job
systemd timers Yes, with journalctl Yes, if the unit pings No, cron lines only
  • The backup, and anything that must succeed: a ping monitor that reports success, plus a check of what the job left behind. Monitoring backup jobs from your logs has both.
  • Everything else cron runs, including the packaged jobs nobody wired up: learn it from the logs.
  • systemd timers: systemctl list-timers and journalctl -u <unit>. Schedules doesn't learn timers.

For one server and one job that didn't run today, start with how to check if cron is running. If it's certbot, certbot not renewing.

Limits

  • It sees the start. A job that runs and fails still logs its CMD line.
  • Logs can be faked. logger -t CRON '(root) CMD (/usr/local/bin/backup.sh)' counts as a run.
  • About a week to learn. Up to 21 days for jobs that skip days.
  • Cron lines only. systemd timers aren't learned.
  • Laptops and desktops never get missed-run alerts. They sleep.

When the whole server stops sending, that's a different problem with different alerts: log stop detection.

Simple, Affordable, Log Management and Analysis.

14 days free trial. No credit card required.