2

I've got a pretty good handle on how cron/anacron work on linux. My question is this:

  1. anacron runs at login, and checks if it's been more than a day since the scripts in /etc/cron.daily have run
  2. anacron runs scripts in /etc/cron.daily, and updates the timestamp in /var/spool/anacron/cron.daily
  3. anacron exits
  4. computer stays on for more than 24 hours
  5. cron runs and detects a time match for cron.daily
  6. cron does nothing since test -x anacrontab" == True

If my computer runs for more than 24 hours, how does cron run the scripts in cron.daily (This is a Debian based distro)?

the default /etc/crontab (which is running every minute) tests for the presence (or more precisely, the -x bit) of /usr/bin/anacron, and ignores the /etc/cron.daily folder if it finds it, but it doesn't seem to restart anacron to do the work.

EDIT:

So cron DOES start anacron from the /etc/cron.d folder, but only if the computer is on and logged in at 7:30 am every day. After doing a little spreadsheet kung-fu, I've come to the conclusion that the scripts in /etc/cron.daily will run anywhere between 1 minute and 31.5 hours apart.

Ex:

Scenario 1:

  • log in at 11:59 pm on 5/23/2021
  • assuming the anacron timestamp for /etc/cron.daily is older than 5/23/2021, anacron runs all the scrips in /etc/cron.daily and updates the timestamp to 5/23/2021
  • log out
  • log back in at 12:00 am on 5/24/2021
  • anacron sees that the last run was 5/23/2021, and it is now 5/24/2021, so anacron runs all the scripts in /etc/cron.daily
  • elapsed time between runs: 1 minute

Scenario 2:

  • log in at 12:00 am on 5/23/2021
  • assuming the anacron timestamp for /etc/cron.daily is older than 5/23/2021, anacron runs all the scrips in /etc/cron.daily and updates the timestamp to 5/23/2021
  • computer stays on and logged in until 7:30 am on 5/23/2021
  • cron runs anacron from its crontab file in /etc/cron.d
  • since the timestamp is 5/23/2021, nothing happens
  • computer stays on and logged in until 7:30 am on 5/24/2021
  • cron runs anacron from its crontab file in /etc/cron.d
  • anacron sees that the last run was 5/23/2021, and it is now 5/24/2021, so anacron runs all the scripts in /etc/cron.daily
  • elapsed time between runs: 31.5 hours
FargolK
  • 1,629
  • 1
  • 12
  • 20
  • Do you have `/etc/cron.d/anacron` and `/etc/cron.daily/0anacron`? That will run `anacron`'s daily tab from a daily cron job (`anacron -u cron.daily`). I'm not sure what you mean by your sixth point. – Kusalananda May 26 '21 at 19:31
  • anacron -u only updates the timestamps, but DOES NOT start anacron. The 0anacron just calls anacron -u. /etc/cron.d/anacron only starts anacron if the comnputer is on at 7:30 am. – cyclopticnerve May 27 '21 at 01:48

1 Answers1

0

All the system /etc/cron.daily, /etc/cron.weekly, etc files include an entry to run anacron which in turn includes an entry to run cron. /etc/crontab includes entries for run-parts so that anacron is either run by run-parts or by cron.

Another file to consider is /etc/anacrontab which can override some of the effects of /etc/crontab

An important system cron related file is /etc/cron.daily/logrotate which is a script used to initiate log rotation processing.

Ordinary user crontabs are executed independently of system cronjobs.

Note that /etc/crontab is not running every minute - activity is determined by the various crontab specifications.

Tom Newton
  • 79
  • 1
  • 6
Jeremy Boden
  • 1,290
  • 11
  • 21
  • 1
    /etc/cron.{daily, weekly, monthly} do include 0anacron, which updates the timestamps, but DO NOT start anacron. /etc/cron.d will start anacron, but only at 7:30 am. – cyclopticnerve May 27 '21 at 01:26