cron & anacron

Both cron and anacron serve the same purpose: schedule recurring jobs. The differences lie in that Anacron is only executed with a maximum frequency of once a day (as opposed to once a minute for Cron) and can run even when its start time was missed (e.g. system down).
Both schedulers come by default in most installations, but if they do not, they can be installed with the command:

# yum install cronie cronie-­anacron
# systemctl start crond.service
# systemctl enable crond.service

The configuration file for anacron is  /etc/anacrontab and it should look something like this:

# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
# the maximal random delay added to the base delay of the jobs
# the jobs will be started during the following hours only
#period in days        delay in minutes      job­identifier       command
1                      5                     cron.daily          nice run­parts /etc/cron.daily
7                      25                    cron.weekly         nice run­parts /etc/cron.weekly
@monthly               45                    cron.monthly        nice run­parts /etc/cron.monthly

The RANDOM_DELAY parameter states the random number of minutes that can be added on top of the “delay in minutes” specified for each job. By default it is set to 6 minutes but it can be set to 0. So if the RANDOM_DELAY is set to 10 and the “delay in minutes” of cron.daily is set to 5, then between 5 and 15 minutes will pass before the job is executed.
The START_HOURS_RANGE determines the hours in which jobs can be run.
The “period in days” states the frequency with which jobs are executed. It can be set to @daily (or 1), @weekly (or 7) or b.
With the configuration laid out above…

• all the scripts in /etc/cron.daily would be executed once a day between 03:00 and 22:00 with a delay of between 5 and 50 minutes

• all the scripts in /etc/cron.weekly would be executed once a week between 03:00 and 22:00 with a delay of between 25 and 70 minutes

• all the scripts in /etc/cron.monthly would be executed once a month between 03:00 and 22:00 with a delay of between 45 and 90 minutes

If we need to execute just some of the scripts in a directory specified with the run-parts command, we can do so by creating jobs.allow/jobs.deny files in that same directory. If we create the jobs.allow file, only the scripts listed will be executed ( jobs.deny will be ignored). If jobs.allow does not exist, then jobs.deny will be checked to see what scripts should be skipped.

If we want to disable anacron completely, as we can’t do it with the ubiquitous systemctl command because anacron runs on the back of crond, we should add the line “0anacron” in /etc/cron.hourly/jobs.deny.


The master configuration file for cron is /etc/crontab and it looks something like this:

# cat /etc/crontab
# For details see man 4 crontabs
# Example of job definition:
# .­­­­­­­­­­­­­­­­ minute (0 ­ 59)
# | .­­­­­­­­­­­­­­ hour (0 ­ 23)
# | | .­­­­­­­­­­­­ day of month (1 ­ 31)
# | | | .­­­­­­­­­­ month (1 ­ 12) OR jan,feb,mar,apr …
# | | | | .­­­­­­­­ day of week (0 ­ 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user­name command to be executed
1 4 * * 1 rsync /usr/local/bin/ > /var/log/rsync2nfs.log 2>&1

So the script in the example would be executed at by the rsync user, at 04:01am every Sunday.
By default only root can schedule cron jobs, but that can be changed using the /etc/cron.allow/etc/cron.deny files. The rules are:

• If cron.allow exists, only the users listed on it are allowed to use cron (cron.deny is ignored).
• If cron.allow does not exist, only users listed in cron.deny will be denied access to cron.
• If neither file exists, only root can use cron.
• If a user is allowed to use cron and it schedules jobs with it, we can see the user’s crontab in /var/spool/cron.

We can use vi, vim, nano or any other editor to modify /etc/crontab logged in as root, but it is safer to use crontab -­e (to edit) and crontab -­l (to list) scheduled jobs. The /etc/crontab file is only meant for root.
If we are a non-privileged user we are forced to use crontab -e which will edit our crontab in /var/spool/cron/<username>.

<< Scheduling                  at & batch >>