plists and launchd
Recently I wrote about getting Tomcat running in Mac OS X. You can easily start it up manually anytime you want by running the startup script in /Library/Tomcat/bin after you have it installed. But if you want it or any other daemon to run at startup you can use launchd if you are running Tiger. Launchd is Apple’s newest way to launch startup background processes. It’s really quite simple to make a daemon such as Tomcat or mysql run as a startup item using launchd. It required writing a plist file and putting it into either the /Library/LaunchDaemons folder or the /System/Library/LaunchDaemons. As a starting point, I would open up some of those plists and get a feel for the format. For my plist, I just used 6 key value pairs in the plist. For the most part they are self explanatory. The required key label is a unique identifier that identifies your daemon to lanuchd. Often times it uses reverse domain lookup format, which is what I chose. I could have just called it tomcat. ProgramArguments, also required, are the arguments launchd will use to run the daemon. In this case all I am giving it is the path to the startup script. OnDemand is recommended, but not required. This key specifies whether the daemon launches on-demand or must always be running. I gave it a ServiceDescription, a UserName and a WorkingDirectory all optional. Below is my plist:
<?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.jakarta.tomcat</string>
<key>OnDemand</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/Library/Tomcat/bin/startup.sh</string>
</array>
<key>ServiceDescription</key>
<string>Tomcat Server</string>
<key>UserName</key>
<string>root</string>
<key>WorkingDirectory</key>
<string>/Library/Tomcat/bin/</string>
</dict>
</plist>
I was asked a couple of times if cron could still be used in OS X after Tiger arrived. Apparently people were reading that launchd was replacing cron. Cron is still a viable way for launching scripts at prescribed times. Apparently launchd can handle these tasks as well (check out the daily and monthly plists), but OS X Tiger still has cron. I use cron all the time. Launchd is responsible for the faster boot times you may have noticed in OS X Tiger.