Anacron in Linux

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.

Syntax :

period delay job-identifier shell-command
  • period – this is the frequency of job execution specified in days or as @daily, @weekly, or @monthly for once per day, week, or month. You can as well use numbers: 1 – daily, 7 – weekly, 30 – monthly, and N – number of days.
  • delay – it’s the number of minutes to wait before executing a job.
  • job-id – it’s the distinctive name for the job written in log files.
  • command – it’s the command or shell script to be executed.
The scheduled jobs are listed in /etc/anacron
Anacron job Options:
  • f : Used to force the execution of the jobs.
  • u : Only update the timestamps of the jobs, to the current date, but it won't execute any job.
  • s : Anacron will start a new job, only when all the previously scheduled jobs are executed.
  • n : Executes the job immediately without any delays.
  • d : For the foreground execution of the task this option is used.
  • q : This option gives a compact message about the error. It only works when it is used along with the -d option.
  • V : Prints version information and exit.
  • h : Prints short usage message, and exit.

There are two important variables in the anacrontab file they are:

  • START_HOURS_RANGE – By using this we can set the time range, the job executes only in that mentioned range.

    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 
  • RANDOM_DELAY – By using this we can mention the maximum delay time to execute a process (by default it’s 45). This means that anacron will add x minutes (randomly picked from 0 and 45), and add this to the user-defined delay.
# grep RANDOM /etc/anacrontab 

Example :

The following example executes the /home/cherchertech/ script once every 7 days.

On the day when the job is supposed to execute, if the system is down for some reason, anacron will execute the 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/

To create a custom anacrontab,

echo "2 2 backup /home/cherchertech/" > customanacron

Once you have scheduled your anacron job, you can run the command below to verify its validity.

anacron -T

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.

Anacron job in Foreground

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)

Cron Vs Anacron

Cron and anacron has advantages as well as disadvantages one over the other.

Cron Anacron
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
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions