Understanding when anacron runs scripts at `/etc/cron.monthly`
I'm trying to understand how cron, anacron and systemd play together and in particular trying to understand how anacron schedule the jobs at /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly
This is what i understand at the moment.
- cron is a daemon. It wake ups itself each minute to check if it must run some script
- anacron is not a daemon. It's executed by start-up scripts or by another scheduler like cron or systemd timers.
- systemd is a suite of utilities, being one of these utilities the "timer" that allows to schedule jobs
If anacron is not present and the computer is on at the expected time cron will run the scripts in:
/etc/cron.dailyAt 6:25 every day/etc/cron.weeklyAt 06:47 on Sunday./etc/cron.monthlyAt 06:52 on day-of-month 1
as is configured in the following file and crontab.guru translates to human readable times.
# /etc/crontab: system-wide crontab
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )If the computer is off or anacron is present cron totally ignores those folders.
Other default configuration of cron is
# /etc/cron.d/anacron: crontab entries for the anacron package
30 7 * * * root [ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/invoke-rc.d anacron start >/dev/null; fiwhich means that if anacron is present and systemd is not present, cron will launch anacron every day at 7.30.
If systemd is present its timer will run anacron each hour
$ cat ./
[Unit]
Description=Trigger anacron every hour
[Timer]
OnCalendar=hourly
RandomizedDelaySec=5m
Persistent=true
[Install]
WantedBy=timers.targetThen, each time anacron is executed (each hour/reboot/wakeup in systemd) reads its configuration file
# /etc/anacrontab: configuration file for anacron
# These replace cron's entries
1 5 cron.daily run-parts --report /etc/cron.daily
7 10 cron.weekly run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly run-parts --report /etc/cron.monthlyand for each job identifier in the config reads /var/spool/anacron/<job_identifier> where is recorded when was the last time (ie: 20201201) that a job was launched by anacron.
If the amount of days specified have passed, the job will be ran again.
So the scheduled times defined in /etc/crontab for /etc/cron.daily and so on will not be respected at all.
And now comes the questions:
Jobs will not be executed exactly when anacron is executed but scheduled to be launched based on the parameters of anacrontab:
START_HOURS_RANGE, andRANDOM_DELAY. ¿Is this true?If anacron runs
cron.monthlythe first time on 1/January. 1/February the server is off. 2/February the server is on and the job is executed and20200202is recorded as timestamp. The next time that the job will be executed is 1/March, 2/March any other day? Why?If a job has to be performed on the 5th of each month, or if it is off at that time at the next possible moment (that same day at another time, next restart, or even the next day). I was thinking in two options, but not sure if there is another good option that anyone can explain, trying to avoid installing new stuff:
- Execute the job each day, and handle the "when" logic in the script itself.
- Use systemd timers and startup utilities