Until 10.4, anyway. 10.4 introduced launchd, which replaces cron, and Apple no longer started cron as part of the boot process. I had a lot of maintenance tasks that I was running with anacron via cron though, and while it was 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 here.
Presuming you've used fink to install anacron, you can cause launchd to run anacron every 15 minutes by creating a file called /Library/LaunchDaemons/net.sourceforge.fink.anacron.plist with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.debian.anacron</string>
<key>ProgramArguments</key>
<array>
<string>/sw/sbin/anacron</string>
<string>-s</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>LowPriorityIO</key>
<true/>
<key>Nice</key>
<integer>1</integer>
<key>StartInterval</key>
<integer>900</integer>
<key>WatchPaths</key>
<array>
<string>/sw/etc/anacrontab</string>
</array>
<key>ServiceDescription</key>
<string>anacron service</string>
</dict>
</plist>
Once you've created the file, all you need to do is use launchctl to load it, by typing sudo launchctl load /Library/LaunchDaemons/net.sourceforge.fink.anacron.plist in a Terminal window. Or reboot.
To change the interval, change the 900 above 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 the RunAtLoad key, and don't forget to delete the <true/> argument as well.
If you didn't use fink, change the /sw/sbin/anacron to the path you installed anacron on.
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 load your job.
And if you're using anacron to run the periodic tasks like I am, you can keep them from getting re-run at the regular time by doing the following commands:
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.periodic-daily.plist
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.periodic-weekly.plist
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.periodic-monthly.plist
Then do sudo launchctl list to make sure they aren't showing up.
The plist file is available here.
No comments:
Post a Comment