At previous article I told you about cron, a system program that runs tasks automatically at a specific time. Now we will see how to automate tasks with Anacron. Anacron allows us to overcome one of Cron's weaknesses: If the computer is off when the task is supposed to be performed, it won't be performed again until the next time of year occurs and the computer is on.
In the case of Anacron, When the computer is turned on again, it performs the tasks that were left pending.
How to automate tasks with Anacron
When the computer starts up, Anacron looks for the last time a given instruction was executed and how often it was told to run. Like Cron, the list of pending tasks is saved in a text file called the rather unoriginal anacrontab. However, there is one important difference: In this case, the date is set by establishing a day, the delay in minutes, the job identifier and the command to be executed.
Anacron checks, for each task, whether it has been run within a certain number of days. That number of days is the period specified for that job. If it hasn't run, Anacron waits the required number of minutes and does so. It then records the date, which serves as an indication of when to run it again.
We can find anacron by name or in the cronie package in major distributions.
To run it we must specify the following parameters:
-F: Execution of the task even if it is not on the indicated date.
-you: Changes the date of the jobs to the current day but does not execute them.
-s: Indicates that a task is executed when the previous one finishes.
on: It's like using the -fy parameters
-q: If used with -d it prevents error messages from being displayed.
-t: Tells Anacron where to find the task list.
-T: Checks the task list settings and displays an error message if necessary.
-S Specifies the directory in which timestamps are stored.
In the Anacrontab file we must add the following parameters
SHELL=/bin/bash: Specifies that Bash should be used as the command interpreter.
MAILTO= After the equal sign, we specify an email address to send the bug report. However, this isn't required on desktop distributions.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin: This allows us to not have to add the route to each task.
The syntax for the Anacrontab task is period delay job id command where:
Period: This is the frequency with which the work should be performed. It can be indicated as a time period (@daily, @weekly, or @monthly for day, week, or month) or with numbers (1 for day, 7 for week, 30 for month, and any number for any period of days).
Delay: This is the amount of time to wait before starting. It is expressed in minutes.
Job ID: It is a name assigned to the task to distinguish it from others
Command: This is the command that anacron must run at the specified time.
We also need to create a directory to run the various tasks.
mkdir -p ~/.local/etc/anacrontab: ~/.local/etc/cron.daily ~/.local/etc/cron.weekly ~/.local/etc/cron.daily ~/.var/spool/anacron
This creates the directories where the daily, weekly, and monthly execution scripts and the report of the last anacron execution will be saved.
To tell it to use these folders:
anacron -fn -t ~/.local/etc/anacrontab -S ~/.var/spool/anacron
We edit the configuration file from the terminal
nano ~/.local/etc/anacrontab
We add these lines:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
Two other variables can be configured
START_HOURS_RANGE: To indicate that jobs should start only within a certain time frame.
RANDOM_DELAY: Sets the maximum random delay
This concludes our brief review of two useful command-line tools for automating tasks on our computers. In later articles, we'll see that there are graphical applications that also allow us to do the same.