Creating a simple launchd item on Mac OS X 10.4

From Elvanör's Technical Wiki
Revision as of 17:48, 25 September 2006 by Elvanor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.

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.

A simple launchd item

The following file will 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>