Running applications on startup with Mac OS X
CEO & Founder of Server Density.
Published on the 15th January, 2010.
Our server monitoring application, Server Density, comes with an open source, lightweight monitoring agent that can be installed on Mac OS X and most Linux distributions. The agent comes bundled with an init file that you may install in order to run the agent automatically on startup. Our Linux packages install this automatically as well. We recently needed to have the agent automatically start on Mac OS X, which cannot use this init file and instead uses an application called launchd.
We will assume that the agent is installed in a location such as /usr/local/bin/sd-agent/. To let Mac OS X know which application we want to run on startup and define other metadata, we must create a propery list file, or plist file:
$ cd ~
$ vim com.boxedice.sd-agent.start.plist
For these purposes, a plist file simply contains XML data that instructs Mac OS X which application to run, in what context, and with which arguments. Here is a good example for the Server Density agent:
The UserName field in this example plist file is optional, but you must ensure that this user has execute permission on the main agent file, agent.py, (/usr/local/bin/sd-agent/agent.py in our example).
Next, this file must be copied to /Library/LaunchDaemons/. This folder contains plist files for applications that are to be run on startup:
$ sudo mv com.boxedice.sd-agent.start.plist /Library/LaunchDaemons/
Now comes the fun part! Reboot your Mac OS X machine and test that the Server Density agent has started:
$ python /usr/local/bin/sd-agent/agent.py status
$ sd-agent is running as pid 55.
If the agent reports its PID, congratulations; the Server Density agent will now automatically start when Mac OS X boots!