Run missed cron jobs using anacron

Nov 17, '03 10:57:00AM

Contributed by: SOX

On PowerBooks and other computers that dont stay on overnight, periodic system maintence jobs run by cron don't run, since they are normalliy scheduled for off-use times like 3am. This means things like your locate.db doesn't get updated and your ever-growing system logs never get truncated. You can manually force these to run yourself using periodic, as was discussed in a previous hint.

Another option is to use the unix utility anacron to automatically do this. anacron is a daemon that automatically starts at boot and watches the cron jobs you have scheduled for daily,weekly, and monthly activities. When it notices that cron has failed to perform a scheduled activity (presumably because the computer was asleep), anacron forces the job to run the next time the computer is awake.

It's easy to install using fink, but you must create a configuration file before it will work. One caution: the standard unix documentation for anacron is not quite appropriate for OS X, so be sure to read the straightforward step-by-step Mac-specific documentation found in /sw -> share -> doc -> anacron -> README.Fink

anacron anacron tries to be polite about how it runs its catch-up jobs. For example, it can be set to wait a decent period after you wake your computer before it tries to run a job, and it can be set not to schedule pending jobs so they don't all run at once. For most people, using anacron is simply a matter of installing it with fink, uncommenting a few lines in the default config file and forgetting it: no tweaking ever. You don't need to be a Unix guru; the README.Fink tells you explicity which lines to uncomment to turn it on.

However there is one situation I've had with anacron that I now know how to fix. Sometimes, like when I'm giving a presentation, I want to make sure that anacron does not suddenly come to life, hitting the disk intensively. There are two things you can do: one is you can turn off anacron, which will stay off until you reboot or restart it. Alternatively, you can lie to it and tell it the cron jobs just ran. here's how:

To stop anacron gracefully send it a SIGUSR1 signal


sudo killall -SIGUSR1 anacron
To restart it later without rebooting

sudo anacron -s
You can combine these into a single script to turn it off for just two hours like this

sudo -b sh -c  'killall -SIGUSR1 anacron; sleep 7200; anacron -s'
Alternatively, to "lie" and fake the timestamps to the current date:

sudo anacron -u
Note that once anacron actually starts running a cron job, it's not advisable to stop it in progress. All above commands are polite and will not stop a running cron job from finishing. So be sure to invoke a desired pause in anacron ahead of time.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031111110802488