14

I've been googling in vain to turn up a general explanation of how cron and anacron work together. The man pages are a bit too deep of a dive for me at this point.

I get confused because I add tasks to crontab, and that works, but when I delete them out of crontab, they still run under anacron. I see the tasks in /etc/cron.daily, but I am not sure how they got there. cron is running on my system always, but not anacron. In fact, I see anacron is started by cron!

Basically I'd like a user-level intro on how to add and remove tasks, since the easily googleable answers that tell you to edit crontab clearly are not complete. A link to a tutorial would be fine.

15

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:

  • Thanks, that Digital Ocean tutorial is particularly good, as they often are. Not sure how I missed it, so I appreciate the link. – Organic Marble Nov 12 '16 at 18:04
  • Thanks. By "cron.daily runs anacron everyhour", do you man "cron.daily runs anacron daily" instead? – Tim Oct 31 '18 at 3:20
  • Thanks for this. -- I've removed 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.) My crontab entries (daily, weekly, ...) now all run at the expected times. – Adambean Oct 19 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.