Skip to content

Running applications on startup with Mac OS X

January 15, 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:


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

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!

3 Comments leave one →
  1. Vebjørn Kvalvik permalink
    April 16, 2010 2:27 pm

    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.

  2. Doug Targett permalink
    February 3, 2011 8:44 am

    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 permalink
      February 3, 2011 9:12 am

      Fix should be:

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

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 902 other followers