
With cron, at, or batch, if the PC is not powered on when a job is scheduled to execute, the job will not be executed. With anacron, if the PC is not powered on when an anacron job is scheduled to execute, the job will execute when the PC is powered on. An anacron job can be created by adding an entry to the /etc/anacron file. The anacron file has 4 fields:
- Field 1 = Recurrence period
- Field 2 = Delay
- Field 3 = Job name
- Field 4 = Command
[root@server1 ~]# cat /etc/anacron
7 15 example.weekly /etc/cron.daily/example.sh
Field 1 - Recurrence period
The number in field 1 represents the number of days that will elapse between scheduled jobs.
- 1 = The job will run daily
- 7 = The job will run once every 7 days (weekly)
- @monthly = The job will run once a month
Field 2 - Delay
The number in field 2 represents the number of mintues to wait before starting a job that was missed. For example, if a job was missed because the PC was powered off, you could configure anacron to start the job 14 minutes after the PC is powered on.
Field 3 - Job name
This is the name of the job, and the name should be unique. There will be a file in the /var/spool/anacron directory for each unique job name. For example, if an anacron job named example.job is created, there will be a file in /var/spool/anacron named example.job. The example.job file will contain text for the last DateTime the job was run.
[root@server1 ~]# cat /var/spool/anacron/example.weekly
20160101
Field 4 - Command
This is the command that will be run. In this example, the example.sh script file is being run.
[root@server1 ~]# cat /etc/anacron
7 15 example.weekly /etc/cron.daily/example.sh
Anacron schedule
Unlike the cron utility, individual anacron jobs do not include a date and time when the job will run. Anacron only define if the job should run daily, weekly, or monthly. The /etc/crontab file will include lines that determine when the daily, weekly, and monthly anacron jobs will run. In this example, the daily anacron jobs will run at 6:25 am, the weekly jobs will run on Sunday at 6:47 am, and the monthly jobs will run on the first day of the month at 6:52 am.
[root@server1 ~]# cat /etc/crontab
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 )
Anacron hours range
The /etc/anacrontab file can contain the START_HOURS_RANGE environment variable. This variable defines the hours when an anacron job can be executed. For example, if START_HOURS_RANGE is 3-19, anacron jobs can only be run between 3:00 pm and 7:00 pm.
[root@server1 ~]# cat /etc/anacrontab
START_HOURS_RANGE=3-19
The /etc/anacrontab file also contains a few lines that defines how often scripts in /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly will run.
[root@server1 ~]# cat /etc/anacrontab
1 5 cron.daily run-parts /etc/cron.daily
7 10 cron.weekly run-parts /etc/cron.weekly
@monthly 15 cron.monthly run-parts /etc/cron.monthly
Did you find this article helpful?
If so, consider buying me a coffee over at