Launchd as cron

One of the many new features that was in Mac OSX 10.4 is launchd, a daemon which is given the job of managing daemon, program and script running. This takes the place of many different UNIX programs and replaces them with a unified daemon.

For the majority of end users, the only one of these programs that they might touch is cron. This program allows a user to have a program run periodically such as a backup or the like. The other way users will notice launchd is that it makes start up and log in times faster. In fact the Login Window at start up is not actually doing anything other than moving a progress bar and gives no indication on how long the system should take to start up, other than it shouldn’t reach the end.

Before looking at the exciting features of cron, let’s first look at replacing a simple crontab entry using launchd.
So let’s look at the cron style plist from apples website. [annotated with square brackets]
N.B.: This file goes in /Library/LaunchAgents and will be ran from next login. Also note that this can be any of the three Library locations ~, / or /System/
<?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>com.apple.periodic-daily</string> [What the enrty is called, Apple reccomends a reversed DNS address such as org.themak.cron-job]
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/periodic</string> [the full UNIX path of the program, /usr/bin/open is a good choice]
<string>daily</string> [this is any arguments fed to the program, so to start firefox “-a” and in another <string> ” firefox” (try open -a then the name of the application you wish to run before adding it here)]
</array>
<key>LowPriorityIO</key>
<true/>
<key>Nice</key>
<integer>1</integer>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>3</integer> [run this at 3:15 am]
<key>Minute</key>
<integer>15</integer>
</dict>
</dict>
</plist>

Also launchd will be smart about sleeping macs and not try to run calendar events on them. Launchd can also do far more than a simple cron style calendar based tasks but also can be made to do much more. To get started with launchd I recommend checking out: Mac Geekery’s article on plists., the apple developer page or even an informative article by ArsTechnica. There is also a wealth of information on launchd to be found with google, I haven’t even scraped the surface. Also, try these manpages:
man launchd
man launchd.conf
man launchd.plist
man launchctl

One Trackback

  1. […] has a better tutorial up at: http://madebyalex.org/2006/09/11/launchd-as-cron/. He has made it so that it will start upon startup of the scheduled time is missed because the […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*