Cron job didn't run because the computer was off (anacron)

Cron does not catch up. If the machine is off at 02:00, the 02:00 job never runs. Anacron fixes that for daily, weekly and monthly jobs, and a heartbeat tells you when one was still missed.

You need: a Lateping account, root on a Linux machine with anacron, about 10 minutes.

The short answer

Cron runs a job only if the machine is on at the scheduled minute. There is no catch-up. For jobs that run daily or less often, use anacron: it records the date each job last finished and runs any job that is overdue, after a delay you set. Then ping Lateping after the job, so a day that anacron also missed does not go unnoticed.

Why it happens

Cron wakes every minute and starts jobs whose time matches now. A machine that is off, asleep or rebooting at that minute never sees the match, and nothing is queued for later.

Anacron works from dates, not minutes. Per anacron(8), it checks whether each job has run within its period of n days, and if not, runs it after the configured delay in minutes. After the command exits, it writes the date (not the hour) to a timestamp file in /var/spool/anacron. Once no jobs remain, it exits.

Docs: anacron(8), anacrontab(5).

How to check whether it is happening to you

See when each anacron job last finished. Each file holds a date as YYYYMMDD.

bash
sudo sh -c 'for f in /var/spool/anacron/*; do printf "%s " "$(basename "$f")"; cat "$f"; done'

Validate your anacrontab. It prints errors and exits 1 if the file is wrong:

bash
sudo anacron -T

If a job’s date is older than its period, the job has not been finishing. Look for lines from anacron in the system log.

The fix

Put the job in /etc/anacrontab. A job line has four fields: period in days, delay in minutes, a unique job identifier, and the command.

/etc/anacrontab
SHELL=/bin/sh
START_HOURS_RANGE=3-22
RANDOM_DELAY=10

# period  delay  job-identifier  command
1         10     nightly-backup  /usr/local/bin/backup-wrapper.sh

START_HOURS_RANGE is the window of hours in which jobs may start. If the machine is off for the whole window, the job does not run that day. Keep the window wide on a laptop. RANDOM_DELAY adds up to that many minutes to each delay.

Jobs run one after another, each starting after the previous one finishes.

How to find out next time

Anacron mails job output to root, or to MAILTO if you set it. Few machines deliver that mail. A heartbeat is more reliable: the wrapper pings Lateping when the job finishes, with its exit code.

/usr/local/bin/backup-wrapper.sh
#!/bin/sh
URL="https://lateping.com/p/<check-id>"

curl -fsS -m 10 --retry 3 "$URL/start" > /dev/null

./scripts/backup.sh
status=$?

curl -fsS -m 10 --retry 3 "$URL/$status" > /dev/null

The exit code goes in the URL. 0 records a success. Any other code from 1 to 255 records a failure, and the code shows in the alert.

Make the wrapper executable:

bash
sudo chmod +x /usr/local/bin/backup-wrapper.sh

Create the check with a period of 1 day and a grace period that matches your START_HOURS_RANGE, for example 12 hours. If anacron never runs the job that day, no ping arrives and Lateping alerts after the grace period.

Notes and gotchas

  • Dates, not hours. A job with period 1 runs at most once per calendar day, however many times anacron starts.
  • Debian and Ubuntu. Anacron is usually started from /etc/cron.hourly/0anacron and at boot. To disable it, add 0anacron to /etc/cron.hourly/jobs.deny (see anacrontab(5)).
  • Stale timestamps. Anacron never deletes timestamp files. Remove the file in /var/spool/anacron when you drop a job.
  • Run now. anacron -n runs due jobs at once and ignores delays. anacron -f runs every job and ignores timestamps. Test on a job you do not mind running twice.
  • Environment. Anacron jobs run with a minimal environment. Use full paths in the wrapper.

Create a free check at https://app.lateping.com/login.

Next steps

On this page