0

For example, when the server boots I need to start Tomcat, and I need to start an Android emulator, and then issue some commands to it. These are on the path already. I'm familiar with Linux and understand the basics of Daemons, and I suspect it will be something similar?

1 Answer 1

1

LaunchDaemons are the preferred way to do this in OS X. Each daemon is controlled by a .plist file in /Library/LaunchDaemons (for add-on daemons; the builtin daemons are in /System/Library/LaunchDaemons) which specifies when to start the daemon (e.g. on load, when a connection is received on a TCP port, at certain times, etc), what command and arguments to use to start the daemon, etc. See the Apple developer docs here, and look in /System/Library/LaunchDaemons for examples.

One big warning if you're used to using other startup systems: by default, launchd expects programs it starts to not daemonize themselves. This page suggests starting Tomcat with a launchd plist named /Library/LaunchDaemons/org.apache.tomcat.plist containing:

<?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>Disabled</key>
    <false/>
    <key>Label</key>
    <string>org.apache.tomcat</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Library/Tomcat/Home/bin/catalina.sh</string>
        <string>run</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

(Note that catalina.sh is told to "run", not "start".)

If you need to launch something that cannot be prevented from daemonizing, you'll need to add something like this to the .plist:

<key>KeepAlive</key>
<false/>
<key>AbandonProcessGroup</key>
<true/>

You must log in to answer this question.