cron.monthly behavior different on CentOS 7

Issues related to applications and software problems
Post Reply
steevithak
Posts: 9
Joined: 2014/09/26 22:02:12

cron.monthly behavior different on CentOS 7

Post by steevithak » 2017/09/18 20:20:08

On CentOS 4.x,5.x, and 6.x scripts dropped into the /etc/cron.monthly directory ran once on the first day of the month. I recently started updating our servers to CentOS 7.x and noticed that behavior has changed. The cron.monthly scripts still run once a month but on an apparently arbitrary day (i.e. one of the servers ran the monthly scripts on Sep 5, another one on Sep 14, etc).

How can I cause CentOS 7.x to behave in the same way as prior versions and execute monthly scripts on the 1st of each month?

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: cron.monthly behavior different on CentOS 7

Post by pjsr2 » 2017/09/18 20:57:20

The jobs in cron.daily, cron.weekly and cron.monthly are executed by anacron as defined in its configuration file /etc/anacrontab.
The jobs are not executed on a specific time, day or week or day of month. This is intentionally. The jobs are executed when anacron determines that more than a day/week/month has passed since last execution. This way the jobs are executed even when the computer has been switched off for some time, as is common for laptops and desktops.

If you want a job to be executed on a specific day of the month, you should specify it in a crontab for execution by the cron daemon.

Note that when a computer happens to be switched of at the day and time specified in the crontab file, the task will not be executed by the cron daemon and execution of the task will be skipped. This is different from anacron: anacron will execute the task as soon as the computer is switched on again. So on a laptop or desktop that is frequently switched off, it is safer to use cron.daily/cron.weekly/cron.monthly for recurring tasks like backup or log file rotation.

steevithak
Posts: 9
Joined: 2014/09/26 22:02:12

Re: cron.monthly behavior different on CentOS 7

Post by steevithak » 2017/09/18 21:11:18

Unless I'm mistaken CentOS 6.x also relied on anacron but it was able to execute the /etc/cron.monthly jobs accurately on the first day of each month. Maybe it's just a change in the configuration for anacron? Would copying the /etc/anacrontab from one of our CentOS 6.x boxes be enough to restore the behaviour?

steevithak
Posts: 9
Joined: 2014/09/26 22:02:12

Re: cron.monthly behavior different on CentOS 7

Post by steevithak » 2017/09/18 21:52:17

Nevermind, after more googling, I finally found the answer. The previous poster is correct that anacron was added to make things work better on computers that are frequently turned off (laptops rather than enterprise systems like servers). It's unclear why they made that the default for an enterprise OS as the downside is cron jobs running at essentially unpredictable times. But they provided a simple solution to revert to the previous behavior designed for servers running 24/7. Here are the steps (must be done in this order):

yum install cronie-noanacron
yum remove cronie-anacron
systemctl restart crond.service

Afterwards, the file /etc/cron.d/dailyjobs contains the execution times for the /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly and they will once again run at predictable and controllable times. The default times are identical to previous versions of CentOS with monthly jobs executing at 4:42am on the 1st of the month.

Post Reply