Running applications on startup with Mac OS X

By David Mytton,
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.

Requirements

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:

[sourcecode language=”xml”]
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.boxedice.sd-agent.start</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python</string>
<string>/usr/local/bin/sd-agent/agent.py</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true></true>
<key>UserName</key>
<string>ryanduffield</string>
</dict>
</plist>
[/sourcecode]

Security

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).

Installing

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/

Verifying

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!

  • Vebjørn Kvalvik

    I’ve searched the net a lot for a solution for a similar problem.

    We have the same setup as you do with OS X server 10.6 on a Mac Mini.
    Our POS/CRM-system server is a Unix executable located in /Applications/Folder on the server.
    ( ./Application in folder /Applications/Folder )

    Today I have a AppleScript that runs as a Login Item for the Admin-user, but I want the Unix Exe, to start on boot as I dont wan’t the whole shop to rely on me being at work if the server restarts.

  • Doug Targett

    Hi,

    I had an issue with the agent stopping and having to unload and load the launchctl script to start it off again. have added the following to the dict to fix:

    KeepAlive

    • Doug Targett

      Fix should be:

      <key>KeepAlive</key>
      <true/>

Articles you care about. Delivered.

Help us speak your language. What is your primary tech stack?

Maybe another time