Startup Processes With Launchd

Some jobs are too important to be left to users. While native services like file and print sharing begin when Macintosh systems first boot, third party applications are often left to start as Login Items, configured out of System Preferences on a single account. For essential services, this method can range from unreliable to disastrous.

Introduced in OS X 10.4, launchd allows you to easily convert any program into an automated process. The method can be used for everything from simple shell scripts, like those handling routine maintenance tasks, to full-fledged commercial applications, replacing the legacy startup methods for products like Backup Exec 11d and Oracle 10g.

Upon boot, launchd utilizes configuration files in the plist format, xml-based preferences that can be built in any text editor to store system or application configurations. These files can dictate scheduling, watch file paths for changes, or define network sockets and system resources dedicated to a specific job. At its most basic, however, just a couple of parameters can safely start administrative processes.

<?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>JOB-NAME</string>
<key>ProgramArguments</key>
<array>
<string>APPLICATION-PATH</string>
</array>
<key>OnDemand</key>
<false/>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

In the example above, JOB-NAME should be replaced with a unique identifier in reverse-DNS format (like com.domain.job), while APPLICATION-PATH should be the absolute (full UNIX) path to the command you're launching. If there are additional command line options, they should be listed one at a time, directly underneath the APPLICATION-PATH and inside additional string tags.

With your plist built, save it in /Library/LaunchDaemons with a reverse-DNS filename (such as com.domain.job.plist). Then change it's ownership and permissions to run as the root superuser:

sudo chown root:wheel /Library/LaunchDaemons/FILENAME
sudo chmod 700 /Library/LaunchDaemons/FILENAME

If your process should run as another user or group for security reasons (such as an application-specific account), simply include the UserName and GroupName key after ProgramArguments in your plist file:

<key>UserName</key>
<string>APPLICATION-USER</string>
<key>GroupName</key>
<string>APPLICATION-GROUP</string>

Now applications can launch as any user at startup, run continuously in the background, and automatically restart as needed. There's syntax for reoccurring tasks, intended run order, file permissioning, and an overwhelming host of other options.

For those administering customized or complex OS X installations, a thorough understanding of launchd is essential to a well-designed and well-maintained system.

Recommended Reading: For more information on configuring launchd for advanced tasks, you can read the full launchd.plist manual at Apple's Developer Connection.