How do I launch an app at boot/startup on Sierra?

Solution 1:

Service/app launch while booting is usually done with launchd and launch daemons controlled by launchctl. It depends on the app/executable and the environment if such a launch daemon is possible or not. A lot of GUI apps require a login session and the method doesn't work.

After inspecting GoCD you can either download a zip file or an "OS X/macOS app" version of the server. The server is started with a shell script in both cases: either explicitly in the zip version or implicitly with "Go Server.app/Contents/MacOS/go-server".

Go with the zip file, unzip it and move the resulting folder to /usr/local. Link the folder /usr/local/go-server-16.10.0 to /usr/local/:

ln -s /usr/local/go-server-16.10.0 /usr/local/go-server

Create a launch daemon with sudo nano /Library/LaunchDaemon/org.goserver.start.plistand the content

<?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>Label</key>
    <string>org.goserver.start</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/usr/local/go-server/server.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/org.goserver.start.stderr</string>
    <key>StandardOutPath</key>
    <string>/tmp/org.goserver.start.stdout</string>
</dict>
</plist>

Change owner and permissions of the file if necessary:

sudo chown root:wheel /Library/LaunchDaemons/org.goserver.start.plist
sudo chmod 644 /Library/LaunchDaemons/org.goserver.start.plist

Modify /usr/local/go-server-16.10.0/server.sh to your needs. You should set a different log directory in line 85 or a log file will be created in the root directory. Check other variables and paths (like $JAVA_HOME, YOURKIT_PATH, $PID_FILE etc) in the shell script and modify them according to your needs/environment. Here it worked out-of-the-box - but I didn't test it thoroughly.

Then start the launch daemon with:

sudo launchctl load /Library/LaunchDaemons/org.goserver.start.plist

Check /tmp/org.goserver.start.stderr for launchd errors. If the launch daemon runs well you may remove the part:

    <key>StandardErrorPath</key>
    <string>/tmp/org.goserver.start.stderr</string>
    <key>StandardOutPath</key>
    <string>/tmp/org.goserver.start.stdout</string>

from the plist.

The method should work accordingly with the GoAgent. The start of the agent is done with agent.sh. Use a second plist with a different plist and label name of course: org.goagent.start.plist/org.goagent.start.

You probably have to add a condition in the agent's plist so that the agent is started after the server is running.


I tried the same with the "OS X" app but I failed to eliminate all errors.