Services on OS X are installed, configured, and controlled through the launchctl utility.

For example, to stop sshd: # launchctl stop org.openbsd.ssh-agent

Creating services

Services are configured using plist XML files. A service's plist file can specify, among other things, the script or binary to run, the user to run as, and whether or not the service should be restarted if it crashes.

For example, this is a plist for a script which runs a Jenkins build slave:

<?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>KeepAlive</key>
        <dict>
            <key>SuccessfulExit</key>
            <false/>
        </dict>

        <key>Label</key>
        <string>name.chc.jenkins-slave-wrapper</string>

        <key>Disabled</key>
        <false/>

        <key>UserName</key>
        <string>builduser</string>

        <key>GroupName</key>
        <string>everyone</string>

        <key>ProgramArguments</key>
            <array>
                <string>/opt/jenkins-slave/jenkins-slave-script</string>
            </array>

        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

This plist will keep the script /opt/jenkins-slave/jenkins-slave-script running continuously by the user builduser. The service's label to use with launchctl will be name.chc.jenkins-slave-wrapper.

Installing the plist file

Services to start at boot must have their plist files installed in /System/Library/LaunchDaemons.

To activate the plist file above, save it to /System/Library/LaunchDaemons, change its ownership to root:wheel, and install it using launchctl:

# cp name.chc.jenkins-slave-wrapper.plist /System/Library/LaunchDaemons/
# cd /System/Library/LaunchDaemons/
# chown root:wheel name.chc.jenkins-slave-wrapper.plist
# launchctl load name.chc.jenkins-slave-wrapper.plist

Check on the daemon's status with # launchctl list | grep chc.name.jenkins-slave-wrapper

Check for debugging output in /var/log/system.log.

See also