10.4: Run Anacron via launchd

May 19, '05 08:53:00AM

Contributed by: jpbjpbjpbjpb

Tiger only hintI used to run Anacron on my laptop in 10.3, so that I could run the daily, weekly, and monthly maintenance tasks at appropriate intervals, but without having to have the laptop awake at any specific time. Anacron allows you to specify that specific tasks run every N days, and every time it runs, it checks each job to see if it's been long enough since the last run, and runs it if appropriate. This worked out quite well -- I had Anacron tasks set up to run the daily script every day, the weekly every seven days, and the monthly every 30 days. I had cron set to fire up Anacron every 15 minutes, and all was well.

Until 10.4, anyway. 10.4 introduced launchd, which replaces cron, and Apple no longer started cron as part of the boot process [Editor's aside: See the comments; cron is installed and running, but differently]. I had a lot of maintenance tasks that I was running with Anacron via cron, though. While it would have been easy enough to make an old-style StartupItem to launch cron, I wanted to do the right thing.

Anyway, after experimenting with plutil and launchctl a bit, I figured out how to get launchd to run Anacron every 15 minutes. It isn't all that complicated, if you're familiar with XML, but in the interest of sparing other people the hassle, I'm documenting it in my blog -- the details are also in the remainder of this hint...

Presuming you've used Fink to install Anacron, you can cause launchd to run Anacron every 15 minutes by creating a file called net.sourceforge.fink.anacron.plist in /Library/LaunchDaemons, with this content.

Once you've created the file, all you need to do is use launchctl to load it, by typing this in the Terminal:

sudo launchctl load /Library/LaunchDaemons/net.sourceforge.fink.anacron.plist
Or you can just reboot. To change the interval, change the 300 in the code snippet to whatever number of seconds you want launchd to wait between invocations. If you don't want it automatically running as soon as you boot up in addition to every 300 seconds, delete these two lines:
 <key>RunAtLoad</key>
 <true/>
If you didn't use Fink, change the /sw/sbin/anacron directory to the path where you installed anacron. Finally, if you get an error message when you try to load the file with launchctl, you can use plutil to check the syntax by typing plutil lint org.debian.anacron.plist -- if it isn't exactly correct, launchd will complain and refuse to load your job.

Comments (8)


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