Nov 6, 2013

Task management with Cron + Anacron

What is Anacron

Anacron is used to execute commands periodically, with a frequency specified in days. Its a good partner of any machine with cron jobs that it isn't running 24/7. For each job, Anacron checks if it has been executed in the last n days, where n is the period specified for that job. If the job hasn't been executed, Anacron will execute it.

Anacron configuration

Anacron configuration file is located in /etc/anacrontab and looks like this:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
# format: period delay job-identifier command
##
## If you turn any of the following on, make sure that the
## regular cron daemon does not run them also. This could
## lead to double execution of the jobs.
1       5       cron.daily      run-parts /etc/cron.daily
7       10      cron.weekly     run-parts /etc/cron.weekly
30      15      cron.monthly    run-parts /etc/cron.monthly 

Tasks are represented in the following format:
period  delay   job-identifier  command

The job identifier should be unique for each job, as Anacron uses that name to create a file in /var/spool/anacron with the timestamp of the last time that task was executed.

Conflicts between Cron and Anacron

If the PC is restarted and Cron executes its daily/weekly/monthly routines, it is possible to execute them twice if also Anacron tries to execute them. To avoid this conflict, we can create a file inside the cron folders to update the timestamp of Anacron tasks and avoid running them again.

The simplest example should be to create a script called 0anacron, inside the cron.daily directory, that runs Anacron with the -u option, just to update the timestamps of Anacron's tasks:
#!/bin/bash
/usr/sbin/anacron -u

If you have Anacron configured to execute different tasks in different time frames (such as one daily and one weekly), it is possible to append the task name or job-identifier to the -u option, so Anacron will only refresh that task. This way we can create one script in cron.daily to reflesh the daily Anacron task and one script in cron.weekly to refresh the weekly task.

Anacron on Suspend

Anacron will run automatically everytime you turn on (or reboot) your PC, but what happens when you just suspend it? Anacron won't run when you wake your PC from suspend, but there are a couple of things you can do to avoid this issue:

Use Cron

This might not be the most elegant solution but it works, just create an entry in your crontab that calls Anacron every X hours, where X should be a period short enough that would grant that your PC will be online in at least one period of a whole day. That way all daily tasks should be able to run without problems, for example:
# Run Anacron every two hours
* */2 * * * /usr/sbin/anacron -s

Use pm-utils

You need to configure pm-utils on your kernel, but it is possible to control several events, including wake from suspend. With a custom script on wake from suspend events, we could make a call to Anacron each time the PC wakes up, so we can ensure that all tasks run without problems. You should create a new executable script in /etc/pm/sleep.d. Any script found there on sleep or wake-up routines will be executed, so you can create a script like the following to execute Anacron each time the PC wakes up:
#!/bin/bash
case $1 in
  thaw|resume)
    /usr/sbin/anacron -s
    ;;
esac