Anacron is similar to cron, just that it’s usually used on systems that are not up all the time, such 1
as desktops or laptops. 2
Anacron does not expect the system to be running 24 x 7 like a server. When you want a background job 3
to be executed automatically on a machine that is not always up, you should use anacron instead of 4
cron. 5
For example, if you have a backup script scheduled to run every day at 10:00 PM as a regular cron job 6
and if your laptop is not up at 10 pm, your backup job will not be executed 7
when the system starts, it is skipped until the next schedule. 8
However, if you have the same job scheduled using anacron, you can be sure that it will be executed 9
once your laptop starts. 10
If cron runs as a daemon, anacron is just a command that has to be run in order to execute the scheduled 11
tasks. 12
In fact anacron is run automatically each time the system starts. 13
Also note that anacrone is run by root so the tasks will have privileges and the frequency of the job 14
execution is expressed in days, not in minutes, like in the case of cron. 15
The file which specifies the anacron jobs is /etc/anacrontab. 16
Let's open the file as root! 17
There are already 3 jobs which in fact are related to cron. 18
By the way, run-parts 19
is a command which runs all the executable files found in a directory. 20
For example, this line means that all executable files or scripts found in /etc/cron.weekly 21
will be executed with a frequency of seven days. 22
Let’s see what’s the format of a line which in fact represents an anacron job: 23
There are four fields: 24
The first field 25
is the period in days. It specifies the frequency of the job execution, expressed in days. For example, 26
a 1 (one) in this field means run it every day, and a 7 means run it every 7 days or weekly. 27
In this field you can also use something like @daily, @weekly or @monthly. 28
The second field 29
is the delay in minutesc 30
It specifies how long anacron will wait before executing the job. . 31
The delay feature is intended to keep the system from being overloaded with lots of jobs 32
if anacron determines it needs to run several commands when it starts up. It won’t run them simultaneously, 33
but with a delay. 34
The third field is the job identifier; it should be unique for each job, and it's the name of a file 35
that will be created under /var/spool/anacron directory. 36
This is called the Job Timestamp File and will contain a single line that indicates the last time when 37
this job was executed. 38
This identifier has also the purpose to identify the job in the messages or the log files. And the last 39
field is the command that will be executed. 40
Let's schedule an anacron job. 41
The anacrontab file should be opened by root. And at the end, I am adding a new line, which is in 42
fact a new job. 43
I wanted to run every two days, with a delay of one minute, 44
so one minute after the computer starts or anacron is executed manually; the name of the job will 45
be back up 46
and the job will make a back up, so the script will be /root/backup.sh 47
However, for this example, I'll use a simple command just to check that it works. 48
So date >> /tmp/anacron.txt 49
And I'm saving the file. 50
OK, the file is read-only because it wasn't opened by root, but there is no problem. 51
I'm copying the line to the clipboard, exiting the file without saving. 52
And opening the file again as root! 53
And at the end, I am pasting the line. 54
Perfect. 55
Now I'm saving and exiting the file. Before running anacron, I can check for any syntax error by running 56
anacron-T 57
If there are any errors in the anacron tab file, it will print out a message indicating the error. 58
And as root I'm running anacron-T 59
Don't forget to run it as root; and it says the job back up will be running one minute, 60
so we are waiting. 61
Normally, anacron jobs are run in the background and nothing is printed on the screen. 62
However, when you test it, you want it to run in the foreground so you can see its output; that's what 63
option -T does. 64
It doesn't fork 65
anacron to the background. 66
The name of the job is back up, so the job timestamped file is called Back Up and it was created in/var 67
/spool/anacron. Let's check it in another terminal. 68
sudo cat /var/spool/anacron and you see here the file. By the way, the job was executed. 69
Let's check the contents of the file and you see the timestamp. 70
This is the date when the job was executed. 71
This is how it knows how to execute the job again, in this case tomorrow after a day or when the computer 72
will start if it's down tomorrow, all day long. 73
If I run anacron again, it will say that it has no jobs to run. 74
All the jobs have already been executed!