The cron command executes only when the system is powered on but the Anacron command runs even when the system is power off. This is the major difference between the corn and anacron, anacron is used to run commands periodically with a frequency defined in days. It works a little different from cron; assumes that a machine will not be powered on all the time.
If you have scheduled to run a task at 12:00 AM and your system is in the off state then the scheduled job will not be executed using crontab. To overcome this issue anacron is used.
Anacron won't execute the scheduled job when the system is powered off but it will run the scheduled job the moment system is powered on.
period delay job-identifier shell-command
There are two important variables in the anacrontab file they are:
By default, START_HOURS_RANGE is set to 3-22 in the file. This indicates the time range from 3 a.m to 10 p.m.
# grep START /etc/anacrontab START_HOURS_RANGE=3-20
# grep RANDOM /etc/anacrontab RANDOM_DELAY=45
The following example executes the /home/cherchertech/backup.sh script once every 7 days.
On the day when the backup.sh job is supposed to execute, if the system is down for some reason, anacron will execute the backup.sh script 15 minutes after the system comes back up (without having to wait for another 7 days).
# cat /etc/anacrontab 7 15 test.daily /bin/sh /home/cherchertech/backup.sh
To create a custom anacrontab,
echo "2 2 backup /home/cherchertech/backup.sh" > customanacron
Once you have scheduled your anacron job, you can run the command below to verify its validity.
If there is no error in your configuration, this command will give not output. If you have done any error in scheduling the process the below error will occur.
anacron: Invalid syntax in myanacron on line 1 - skipping this line
When you don't want to change the configurations in the standard anacron file you can use the spool directory.
In the below example, we are using /root/spooldirectory to store all the timestamp information for the jobs that are defined in the /etc/myanacron file.
anacron -S /root/myspool -t /etc/myanacron
The default spool directory is /var/spool/anacron, this stores the timestamp for the jobs from the /etc/anacrontab file. They typically store the information of the job that is recently executed.
Running the anacron in the foreground is easier and we can check if there are any errors while executing the process. We can run an anacron job in the foreground with option -d.
anacron -d -u Updated timestamp for job `cron.daily' to 2021-02-02 Updated timestamp for job `cron.weekly' to 2021-02-02 Updated timestamp for job `cron.monthly' to 2021-02-02
When you want to force the execution of the job you can use the -f option. Though it is a forced execution it will also consider the time day that you have mentioned.
anacron -d -f Anacron started on 2021-02-02 Will run job `cron.daily' in 10 min. Will run job `cron.weekly' in 22 min. Will run job `cron.monthly' in 46 min.
When you want to execute a job immediately use the -n option. This will not consider the time delay that you have mentioned.
anacron -d -n Anacron started on 2021-02-02 Checking against 0 with 31 Normal exit (0 jobs run)
To avoid confusion when you have scheduled n number of jobs, you can give a name to each job so that you can identify them easier when the job executes.
In the following example, it will execute only the job with the name “cherchertech” from the custom /etc/myanacron file.
anacron -f -d -t /etc/myanacron myjob Anacron started on 2021-02-02 Will run job `cherhertech' in 1 min. Job `cherhertech' started Job `cherhertech' terminated Normal exit (1 job run)
anacron has advantages as well as disadvantages one over the other.
|Every minute execution of the job is possible.||Jobs can be executed every day only|
|Cron job is user-independent anyone can schedule the job.||The job can be scheduled only by the super-user.|
|If the system is powered off and a job is about to execute at the time, the job will not be executed.||If a job is scheduled, and the system is power off during that time, it executes the job when the system is turned on.|
|Ideal for servers||Ideal for desktops and laptops|
|Use cron when a job has to be executed at a particular hour and minute||Use anacron when a job has to be executed irrespective of an hour and minute|