Using Anacron in Ubuntu for Periodic Jobs

Anacron is a very handy utility to run scripts in regular periods in machines that are not turned on 24/7. It is actually very simple to use, but I found that there were very few resources describing how to actually set up an Anacron job.

Setting up an anacron job in Ubuntu is as simple as creating a script, name it correctly and assign it the right permissions, then deciding whether it should run hourly, daily, weekly or monthly and then drop it in the right location. Anacron will then run the script at the appropriate frequency.

Let’s explain that in more detail, firstly, the anacron scripts live in the following directories:

/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

When you place a script inside one of those directories, it will run at the specified frequency.

A few gotchas to keep in mind are:

1, Make sure that the script does not contain dots in the name, i.e. do not add an extension. The name of the script can only contain letters, numbers, underscores and hyphens.

2. Apply the correct permissions; the script must be executable and owned by root. You can check the permissions of existing scripts to know what permissions to use:

$ ls -l /etc/cron.daily
total 72
-rwxr-xr-x 1 root root 311 Jun 20 2010 0anacron
-rwxr-xr-x 1 root root 633 Feb 14 2012 apache2
-rwxr-xr-x 1 root root 219 Apr 10 2012 apport
-rwxr-xr-x 1 root root 15399 Mar 13 2013 apt
-rwxr-xr-x 1 root root 314 Aug 8 2011 aptitude
...

3. If your anacron jobs are not running and you are using a laptop, make sure that the laptop is connected to the mains; as a default in Ubuntu Anacron doesn’t run when the laptop is on battery.

4. To find out when each script is scheduled to run:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

This shows that the hourly scripts run at 17 minutes of each hour, the daily scripts run at 6:25am, the weekly scripts run on Sunday and the monthly scripts run on the first of each month at 6:52am.

5. The above schedules are really only the earliest time the script will run. If the machine wasn’t powered on when the script was due to run; it will run at the next opportunity based on the following:

$ cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# These replace cron's entries
1 5 cron.daily nice run-parts --report /etc/cron.daily
7 10 cron.weekly nice run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly nice run-parts --report /etc/cron.monthly

The first column indicates the frequency in days and the second specifies a delay for how many minutes should anacron wait to start running the scripts after the machine starts. So, in the above example, the anacron tab specifies that the daily scripts will run once a day and will wait 5 minutes before start running. The weekly scripts will wait 10 minutes before start running and the monthly ones will wait 15 minutes.

Leave a Reply

Your email address will not be published.