0

On Ubuntu, what is the calling relation between cron and anacron? I am confused by looking at /etc/anacrontab, /etc/cron.daily/0anacron and /etc/crontab.

From https://askubuntu.com/a/848638/1471

cron.daily runs anacron everyhour.

What does "cron.daily runs anacron everyhour" mean?

Is it "cron runs anacron daily via cron.daily" instead? Thanks.

Tim
  • 101,790

2 Answers2

3

You are quoting the text wrong. It says cron.daily runs anacron every day (the text may have been updated since). Also that is exactly what it means.

anacron is not (regardless of what the AskUbuntu answer says) a daemon. It's a small program that needs to be invoked by cron on a daily basis (or more often, though it won't make any difference) to run jobs specified in /etc/anacrontab.

Kusalananda
  • 333,661
2

The anacron manpage describes what it’s supposed to do, and to some extent how it does it. You should trust that, and the contents of the files on your system, more than information you find on unrelated sites on the Internet (including this answer of course).

anacron’s job is to ensure that daily, weekly and monthly jobs are run periodically, even if the system isn’t running at the appropriate times. It doesn’t need cron to operate but it does need to avoid duplicating cron’s work.

/etc/anacrontab tells anacron what to run; by default, that’s all the cron jobs defined in files under /etc/cron.{daily,weekly,monthly}, at the appropriate intervals. When anacron is run (in a mode where it is asked to actually run the jobs it manages), it checks to see when the jobs were last run, and only runs them if a period of time consistent with the jobs’ intended periodicity has elapsed.

/etc/cron.{daily,weekly,monthly}/0anacron take care of ensuring that anacron is aware of cron’s operation: every time cron processes its daily, weekly, and monthly jobs, the first job it runs updates anacron’s timestamps, resetting the counter for the elapsed time since the last execution of the corresponding set of jobs.

/etc/cron.d/anacron and /lib/systemd/system/anacron.timer ensure that, on systems with either cron or systemd installed, anacron is run periodically: daily with cron, hourly with systemd. anacron is also run at system boot (via its init script) and when the power status changes (on resume, or when AC power is connected).

Stephen Kitt
  • 434,908
  • Thanks. (1) I heard that anacron is supposed to be run once every day, because the finest level at which a job can be scheduled by anacron is hourly. Why would systemd want to run anacron hourly? (2) Ubuntu has both cron and systemd installed. Is anacron run both daily by cron and hourly by systemd? Isn't daily enough? – Tim Oct 31 '18 at 16:48
  • The systemd timer runs anacron hourly to reduce the delay between resuming a suspended system and the next anacron run. 2. If systemd is running, anacron’s cron job doesn’t run anacron, so it’s only run by systemd. See point 1 for your daily question.
  • – Stephen Kitt Oct 31 '18 at 17:03
  • Thanks."/etc/cron.{daily,weekly,monthly}/0anacron take care of ensuring that anacron is aware of cron’s operation". On Ubuntu, /etc/crontab says that cron will not run the executables in /etc/cron.{daily,weekly,monthly} on systems with anacron installed. So does cron need to "ensure that anacron is aware of cron's operation"? ("/etc/anacrontab tells anacron what to run; by default, that’s all the cron jobs defined in files under /etc/cron.{daily,weekly,monthly}") – Tim Oct 31 '18 at 17:33
  • It’s a belts-and-suspenders approach. The default configuration, as you say, relies on anacron to take care of daily/weekly/monthly jobs, and cron doesn’t run them; but anything in /etc is modifiable by the system administrator, so /etc/crontab can’t be relied upon to not run cron jobs using cron even when anacron is installed. – Stephen Kitt Nov 01 '18 at 14:06