Creating a simple launchd item on Mac OS X 10.4
Launchd is the new service launcher / startup script system developped by Apple in OS X 10.4. I think it is powerful and very modular. Unfortunately there is few documentation available for launchd yet. The best documentation available is the one of the official man pages (man launchd, man launchd.plist, and man launchctl).
Basic principles
launchd is a daemon running on your system. It scans for launchd items in /Library/LaunchDaemons as well as other places (some are in /System and should probably not be modified). A launchd item is a XML file, containing a number of properties that will affect launchd behavior on this item.
Note that launchd items don't have the notion of dependencies as in most other startup scripts. Dependencies are handled automatically in another way that is currently not very clear to me.
At runtime, launchd can be controlled via the program launchctl. Usual commands are load, unload, start, stop, etc. These are documented in the launchctl manual (man launchctl).
A simple launchd item
The following file would launch the sampledaemon program at startup and keep it running. Note that on the ProgramArguments array, the first item should be the name of the program. Inside the other items (corresponding to command line arguments), don't use space (eg use -p212, not -p 212).
<?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>net.gondolin.sampledaemon</string> <key>OnDemand</key> <false/> <key>Program</key> <string>/usr/local/bin/sampledaemon</string> <key>ProgramArguments</key> <array> <string>sampledaemon</string> <string>-d</string> <string>-l325</string> <string>-p212</string> </array> </dict> </plist>
Notes
To make the job launch as a non-root user, include the key 'UserName' in the plist file. Eg:
<key>UserName</key> <string>www</string>