osx startupitems shell script does not launch appl

2019-08-15 17:44发布

问题:

I'm trying to launch a faceless server application with it's associated project using shell script on OSX 10.10.4.

The shell script has been set to executable.

On Startup nothing happens to launch Wakanda\ Server.app/Contents/MacOS/Wakanda\ Server.

Please help me make this work.

The shell script is at:

Macintosh HD:Library:StartupItems:DispatchStartup:DispatchStartup.sh

The contents of this shell script is:

#!/bin/sh
. /etc/rc.common

# The start subroutine
StartService() {
    # Insert your start command below.  For example:
    /Applications/Wakanda\ Server.app/Contents/MacOS/Wakanda\ Server --solution=/Applications/Dispatch/Dispatch\ Solution/Dispatch.waSolution
    # End example.
}

# The stop subroutine
StopService() {
    # Insert your stop command(s) below.  For example:
    killall -TERM /Applications/Wakanda\ Server.app/Contents/MacOS/Wakanda\ Server
    sleep 15
    killall -9 /Applications/Wakanda\ Server.app/Contents/MacOS/Wakanda\ Server
    # End example.
}

# The restart subroutine
RestartService() {
    # Insert your start command below.  For example:
    killall -HUP /Applications/Wakanda\ Server.app/Contents/MacOS/Wakanda\ Server
    # End example.
}

RunService "$1"

//-------------------------------------------------------------------

// next to the shell script is StartParameters.plist //--------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
    <dict>
        <key>Description</key>
        <string>Wakanda Server</string>
        <key>OrderPreference</key>

        <string>Late</string>
        <key>Provides</key>
        <array>
                <string>Web service to database and objects</string>
        </array>
        <key>Uses</key>
        <array>
                <string>Network</string>
        </array>
    </dict>
</plist>

回答1:

Startup items have been deprecated in favor of launch daemons since OS X v10.4, and they seem to have finally been disabled entirely in v10.10. The better option is... create a launch daemon instead. It'll be a property list (.plist) file in /Library/LaunchDaemons/ containing instructions about what to launch and when to launch it.

This'll be a little more complicated than usual because the launchd system likes to keep track of the jobs it's launched, which requires that they not drop into the background, and I don't see any to prevent Wakanda server from backgrounding itself. You can work around this by adding instructions to the .plist to not keep it alive, and to "abandon" its process group (i.e. not kill off any leftover background processes it spawns). There might also be a problem in that there's no good way to tell it to wait until the network's up. But this is mostly a problem if it tries to listen on specific IP addresses or interfaces; if it just listens on 0.0.0.0 (i.e. all IPs on the computer), this isn't a problem because it'll just pick up interfaces as they come up.

I think the .plist will look something like this:

<?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>Disabled</key>
        <false/>
        <key>Label</key>
        <string>local.wakanda-server</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Applications/Wakanda Server.app/Contents/MacOS/Wakanda Server</string>
                <string>--solution=/Applications/Dispatch/Dispatch Solution/Dispatch.waSolution</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
        <key>AbandonProcessGroup</key>
        <false/>
</dict>
</plist>

put it in /Library/LaunchDaemons/local.wakanda-server.plist, set ownership to root:wheel, permissions to 644, and then either reboot or load it manually with sudo launchctl load /Library/LaunchDaemons/local.wakanda-server.plist.