How to run commands periodically with anacron on Linux

When we need to schedule a task on a Linux system we can use utilities like cron or systemd-timers. Various implementations of cron exist, but they have in common the fact that they run as a system service, and are designed to be used on systems which are always up and running, like servers. When we need to schedule a task on a desktop or a notebook, which are turned off more often, we can use anacron instead.

In this article we learn what is anacron, what are the differences between anacron and cron, and how to use anacron to schedule commands on Linux.

In this tutorial you will learn:

  • How to install anacron on some of the most used Linux distributions
  • What is anacron and what are the differences between anacron and cron
  • How an anacrontab is structured
  • How to run anacron as an unprivileged user
How to run commands periodically with anacron on Linux
How to run commands periodically with anacron on Linux
Software requirements and conventions used
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software anacron,cron
Other None
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux-commands to be executed as a regular non-privileged user

Installation

Anacron (and cron), are usually installed by default on every Linux distributions, however, if for a reason or another we need to install them explicitly, we can use the following commands. On Fedora and the other distributions which are part of the RHEL family, we use the dnf package manager to perform the installation of the “cronie” and “cronie-anacron” packages (cronie is the cron implementation used on those distributions):

$ sudo dnf install cronie cronie-anacron



On Debian and other debian-based distributions, to perform the same operation, we use the following command:

$ sudo apt install anacron cron

On Archlinux, cron and anacron are not installed by default, since the distribution uses systemd-timers as the default method to schedule tasks. Install the packages however, is just a matter of launching the Pacman package manager, issuing the following command:

$ sudo pacman -Sy cronie

Anacron vs Cron

So, what are the main differences between anacron and cron? First of all cron runs as a daemon: it runs in the background and is meant to schedule jobs on systems which are continuously running. With cron we can fine-tune jobs execution times in the order of minutes. Anacron, on the other hand, is not a daemon, is a command which is meant to be invoked directly: it is useful to schedule jobs on machine as desktops or laptops, which are not guaranteed to be always up and running. With it we can schedule jobs which should run at maximum one time per day.

How anacron works

Anacron basically works this way: when launched, it reads jobs scheduled in the anacrontab, which is to anacron what a crontab is for cron. It checks the frequency in days for each job and checks if that job has been executed in the amount of days specified: this is done by storing jobs timestamps in the so called spooldir, which, by default, is /var/spool/anacron. If a job has not been launched in the specified number of days (say for example, the machine was turned off), it is executed after a certain amount of minutes of delay.



As we said before, anacron is not a daemon, so it must be invoked as a command. In the vast majority of cases the invocation of anacron is scheduled via cron, hourly. On Fedora, for example, anacron is invoked via the /etc/cron.hourly/0anacron script.

The anacrontab

The anacrontab structure is different from the classic crontab. The default anacrontab used by cron is usually /etc/anacrontab, but as we will see later in this article, other can be specified when launching anacron. An anacrontab is composed by four columns, which are used to specify, in order:

  1. The job frequency in days
  2. The delay to use for the job in minutes
  3. The job identifier
  4. The command to execute

As an example, just take a look at the content of the default anacrontab on the latest version of Fedora:

# /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

As we can see, aside from the jobs specifications, the anacrontab contains also some environment variables. The SHELL and PATH variables are pretty self-explanatory: they identify which shell should be used as interpreter and the PATH to be used, respectively.

The MAILTO variable is used to specify the mail address to which a mail containing commands output must should be sent to (the default is to send the email to the owner of the anacrontab, root in this case). For this to work, a service like sendmail must be configured on the system. To disable this functionality we can just assign an empty value to the variable (e.g MAILTO=””).



The RANDOM_DELAY variable specifies the maximum number of minutes which are randomly added to each job delay; to disable this feature we can simply set 0 as a value. Finally, the START_HOURS_RANGE variable is used to define an hour interval in which jobs are allowed to run. Here we should choose range of hours during which we are sure the machine will be turned on.

Let’s see how jobs are specified. In the first column, as we already saw, we can find the frequency of execution for each job, specified in days. Here we can use an integer directly or use a macro like @daily, @weekly or @monthly.

The second column contains the delay to be used for the job, specified in minutes. The delay is applied when anacron is invoked and decides that the command should be run.

The third column contains the identifier which is used to refer to the tasks in log files. Finally, the fourth and last column contains the command which should be executed. This can be a direct command, or the path to a script.

How to schedule jobs with anacron as non-privileged users

As we already mentioned in this tutorial, commands scheduled with anacron are executed as root, and are defined in the system anacrontab, which is /etc/anacrontab. Jobs timestamps, instead, are stored in the /var/spool/anacron directory. To be able to use anacron as an unprivileged user, we need to create the per-user equivalent of those files. Our jobs schedule will be saved in to the ~/.local/etc/anacrontab directory, and we will create and use the ~/.local/var/spool/anacron directory to store timestamps of jobs defined in it:

$ mkdir -p ~/.local/var/spool/anacron

As a shorthand we can copy the system anacrontab locally and modify it as we like:

$ mkdir -p ~/.local/etc && cp /etc/anacrontab ~/.local/etc

We may want to change the value of some of the variables contained in the anacrontab, such as that of MAILTO. We can now define our jobs schedules inside of the local anacrontab. As a last step, we need to make sure that anacron is invoked hourly. To accomplish the task we can create an entry in our local crontab. To edit our local crontab we run the following command:

$ crontab -e



As soon as we run the command above, our personal crontab is opened with our default text editor. To run anacron at the beginning of each hour, inside of it, we add the following line:

0 * * * *  /usr/sbin/anacron -s -t "${HOME}/.local/etc/anacrontab" -S "${HOME}/.local/var/spool/anacron"

In the entry, we invoke anacron with the -s option, so that the scheduled jobs are run sequentially, one after the other. To specify the file we want to use as anacrontab we use the -t option and pass the path of the file as argument. Finally, we use the -S option to specify the directory which should be used to store jobs timestamps.

Conclusions

In this tutorial we learned how to use anacron to schedule jobs on systems which, unlike servers, are not guaranteed to be always running. We briefly saw the differences between anacron and cron, how to install anacron on some of the most used Linux distributions, how anacron works and how an anacrontab is organized. Finally, we saw how to implement a non-privileged, per-user anacron instance.



Comments and Discussions
Linux Forum