Well, this guide will focus on scheduling tasks using anacron in Linux/Unix systems.
So what is anacron?
Anacron is a service that can be used to execute scheduled tasks or jobs or commands periodically in Linux/Unix systems with a frequency specified in days.
Scheduling tasks using anacron in Linux/Unix
In our previous guide, we discussed how to schedule system tasks or jobs in Linux/Unix using the
cron command. Check the link below;
How to Schedule Cron Jobs/Tasks in Linux/Unix
So, how does
cron services differ?
cronis suitable for scheduling tasks on systems that are rarely shut down,
anacronis suitable for scheduling asynchronous tasks on systems that are shut down regularly.
- Cron jobs are executed at the exact time they are scheduled and if the system is shutdown when the cron job is supposed to be being executed, that cron is skipped until the next schedule.
- Anacron jobs will be postponed until when the system is running if the system is shutdown by the time they were scheduled to be executed.
- Anacron jobs are executed at most once a day while cron jobs can be executed more than once in a day.
Files used by Anacron
- Anacron reads scheduled jobs from
- It also uses the
/var/spool/anacrondirectory for storing timestamp files on when a specific job was last executed.
Anacron Job Entry Format
Each anacron job entry is specified in the format;
period delay job-identifier shell-command
periodspecifies how frequently, in days the job should be executed. For a example, a value of 10 means the job should be run every 10 days.
delayspecifies a time in minutes between the when the anacron starts and when the job is executed.
job identifierspecifies a unique job ID. This is used in log messages to identify the jobs.
shell commandspecifies the command or the name of the script to be executed.
Anacron operates by checking whether a scheduled job has been executed in the last days specified in the
period parameter. If the job has not been executed, anacron waits for the number of minutes specified in the
delay field to elapse after which it runs the specified shell command or script.
Every anacron job that is being executed or that is awaiting execution is locked so that if there are any other copies of Anacron in the system, such tasks cannot be executed at the same time.
Once the execution of the specified task or job completes, Anacron timestamps this and exits when there is no more scheduled jobs to be executed.
Output from Anacron tasks are mailed to the root user or to the user specified by the MAILTO environment variable if it is defined.
By default, Anacron jobs cannot be executed if the system is not plugged in to power source. This ensures that the battery is not drained when executing the scheduled anacron jobs.
Note that anacron jobs can also be called via the cron jobs.
Scheduling tasks using
anacron in Linux/Unix
Before we can learn how to schedule an anacron job, let us have a look at a sample Anacron table, also known as,
anacrontab file from a CentOS 7 system.
# /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #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
Based on the format above, for example, to schedule an anacron job to execute a;
- backup shell script called
- with a unique identifier of
- with a
delay of 10 minutesbetween when anacron runs and when the script runs,
Simply add the line below on the
7 10 my-backups /path/to/db-backups.sh
To use custom anacrontab for example, you can create is as follows;
echo "1 1 backup /home/me/myscripts/backup.sh" > mycustomanacrontab
Examples on Scheduling tasks using anacron in Linux/Unix
To wrap up with, let us see example usage of various
anacron command line options.
Anacron Command Syntax
The anacron command line syntax is like;
anacron [-s] [-f] [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job] ... anacron [-S spooldir] -u [-t anacrontab] [job] ... anacron [-V|-h] anacron -T [-t anacrontab]
To explain the usage of some of these options, let us see a few examples;
Test the validity of anacron configuration
Once you have schedules 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.
Assume you install an anacron task on your anacrontab called
myanacrontest on your home directory with the line below missing the
1 0 /home/me/myscripts/backup.sh
Run the validity test by running;
anacron -T -t myanacrontest
anacron: Invalid syntax in myanacron on line 1 - skipping this line
Force execution of anacron job
You can force the execution of anacron job by using option
-f. For example, to force the execution of anacron job installed on a custom directory and sent the informational messages to standard error while being mailed to the default user;
anacron -d -f -t myanacron
Run anacron jobs one after the other
To execute anacron jobs one after the first one completes, you can use the
-s options where:
-nmeans execute the jobs immediately
-smeans serialize execution of jobs. Anacron will not start a new job before the previous one finished.
anacron -d -n -t myanacron
anacron -d -s -t myanacron
Both give the same output;
Anacron started on 2019-09-05 Normal exit (0 jobs run)
That is just it on our guide on scheduling tasks using anacron in Linux/Unix. Feel free to read more about anacron command line options on
In our next guide, we will learn how to schedule tasks using the
at command. See the link below;
Scheduling tasks using at command in Linux
Thanks a lot