Launching background processes at startup
Posted on March 9th, 2010 by AVD
The preferred way of launching background processes in Mac OS X means using launchd by creating LaunchDaemons and LaunchAgents which are simple plist (Property List) files which instruct launchd how to start or stop these processes. The important difference in the two is that LaunchDaemons are intended for processes which should remain running even with no users logged into the system.
The common property list format is:
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>program ID</string>
<key>OnDemand</key>
<false/>
<key>Program</key>
<string>program executable path</string>
<key>ProgramArguments</key>
<array>
<string>program executable path</string>
<string>program parameter #1</string>
<string>program parameter #2</string>
<string>…</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
For example, to launch svnserve replace bold text with following:
program ID:
org.tigris.Subversion
program executable path:
/usr/bin/svnserve (or your custom path)
program parameters (each in separate line enclosed with ‘string’ tag):
-d
–root=path to root
–log-file=path to log file
Save resulting file at path “/Library/LaunchDaemons/org.tigris.Subversion.plist”.
We’re now ready to make sure it will work. If you’ve got the svnserve daemon running, open up the Activity Monitor and locate the svnserve process. Select it and press the Quit Process button in the Activity Monitor toolbar. You should be asked for your administrator password. When the process exits it will disappear from the list.
After the process has closed, switch to the Terminal. We’re ready to test our LaunchDaemon to start it up again. In the Terminal, type the following:
Enter your administrator password. You should be returned to a new prompt in the shell if everything goes well.
To verify that our process is registered with launchd, we can print out a list of all the processes run with launchctl by running:
You should see the org.tigris.Subversion item in the list. You can further test that the LaunchDaemon works by simply restarting your system and again checking the Activity Monitor to verify that the svnserve process is running.
Categories: General