Anacron is a very useful tool for systems that aren’t left on constantly. It works in conjunction with cron to make sure that your scheduled jobs get run, even if your computer is off when they are supposed to run. Essentially, anacron will wait for a set period after your computer is turned back on, then it will run any jobs that were supposed to be run. Tux Radar has a great, in-depth article about how they interact.
I personally use anacron on my HTPC (an Acer Aspire Revo), which is running XBMC 9.11, which is based on Ubuntu 9.10. Anacron was not installed by default, so if you are running a similar setup, you can install it by running the following from the command line:
sudo apt-get install anacron
I am in the process of setting my HTPC to go into standby when I’m not using it, as opposed to leaving it on all of the time. Obviously, having the HTPC in standby most of the time would interfere with my cron jobs. One job in particular – updating my XBMC media library – is important, and needs to be run daily. Luckily, anacron comes setup, out of the box, to run not just when you reboot the machine, but also when it resumes from standby. Unfortunately, an issue exists that was preventing anacron from running – both at startup and when resuming from standby.
It turns out that anacron will not run if your computer is operating on battery power. I’m assuming that the idea was that you don’t want to burn precious laptop battery power on background tasks. But my HTPC was reporting that it wasn’t on AC power, even though it is incapable of operating any other way! This was preventing anacron from running my jobs. I was able to see the behavior by parsing my /var/log/syslog:
Jul 9 11:39:29 ***** anacron[2577]: Anacron 2.3 started on 2010-07-09
Jul 9 11:39:29 ***** anacron[2577]: Will run job `cron.daily’ in 5 min.
Jul 9 11:39:29 ***** anacron[2577]: Jobs will be executed sequentially
Jul 9 11:39:29 ***** init: anacron main process (2577) killed by TERM signal
As you can see, the anacron process was being started, and then immediately killed, because the HTPC thought that it was running on battery. After a little more digging, I determined that the culprit was a script named on_ac_power. On my system, this script was located in two locations: /sbin/ and /usr/bin/. To fix the issue, I simply replaced both instances with my own on_ac_power script which contains the following 2 lines:
#!/bin/sh
return 0
This simply returns the value 0, which corresponds to the computer being on AC power. Make sure to set the ownership for the file to root:root and the permissions to 755. After making the replacement, anacron worked like a champ!