Both cron
and anacron
are daemons that can schedule execution of recurring tasks to a certain point in time defined by the user.
The main difference between cron
and anacron
is that the former assumes that the system is running continuously. If your system is off and you have a job scheduled during this time, the job never gets executed.
On the other hand anacron
is 'anachronistic' and is designed for systems that are not running 24x7. For it to work anacron
uses time-stamped files to find out when the last time its commands were executed. It also maintains a file /etc/anacrontab
just like cron
does. In addition, cron.daily
runs anacron every day. Hence, anacron
can only run a job once a day, but cron
can run as often as every minute.
From man anacrontab
:
When executed, Anacron reads a list of jobs from a configuration file,
normally
/etc/anacrontab (see anacrontab(5)). This file contains the list of jobs that
Anacron controls. Each job entry specifies a period in days, a delay in minutes, a unique job identifier, and a shell command.
For each job, Anacron checks whether this job has been executed in
the last n
days, where n is the period specified for that job. If not, Anacron runs the
job's shell command, after waiting for the number of minutes specified as the
delay parameter.
After the command exits, Anacron records the date in a special
timestamp file
for that job, so it can know when to execute it again. Only the date is used
for the time calculations. The hour is not used.
This means, if a task is scheduled to be run daily and the computer was turned off during that time, when anacron is run, it can see that the task was last run more than 24 hours ago and execute the task correctly.
For example if you specify the following in /etc/anacrontab
:
7 15 test.daily /bin/sh /home/username/script.sh
and on the day when the script.sh
job is supposed to executed, if the system is not running, anacron
will execute the script.sh
15 minutes after the system comes back up.
Few Reference:
anacron
from all my VPSes as they're always-on systems. (No idea how it got there in the first place, I'd not heard of it.) Mycrontab
entries (daily, weekly, ...) now all run at the expected times. – Adambean Oct 19 '19 at 09:20