Johnny Gamez
Johnny Gamez

Reputation: 259

How can I create a daemon/launch-agent/background application for OSX?

I'm not exactly sure what I'm looking for qualifies as... an agent,daemon, or just a small background application that runs with no GUI.

I have a small obj-c program I have written that runs in the Terminal. It needs to constantly check for values in a database and makes about 2 calls a second. I already have it working, I'm just not sure how to build this thing for release...

Ideally, I would like users to be able to install it like a regular Mac application but I'm not sure if this is possible. I am looking for something similar to the way Dropbox runs in the background and notifies the user of a new file, or how LogMeIn has something that runs in the background, or I also have an application called Aurora that has a background process called "Aurora Wakeup Helper". These are examples of what I am trying to imitate with this small obj-c program.

How would I build this for release in Xcode 5?

Upvotes: 3

Views: 5056

Answers (1)

Dmitry
Dmitry

Reputation: 7340

To run your app as daemon or agent you should create a .plist file and put it to /Library/LaunchDaemons or /Library/LaunchAgents. Example .plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.example.daemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/me/daemon</string>
        <string>-flag1</string>
        <string>-flag2</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>SessionCreate</key>
    <true/>
    <key>UserName</key>
    <string>mrDaemonUser</string>
</dict>
</plist>

You could find more info in Daemons and Services Programming Guide.

To install your app on user machine you should create an Installer package and write some scripts to put your files in a right place on user's system. For more info see this thread: Making OS X Installer Packages like a Pro.

Upvotes: 6

Related Questions