MacOS – How to run a non-brew app on startup in macOS Sierra

launchdmacos

I had to install an older version of Elasticsearch (2.3.3) from source. The binary is located in /opt/local/elasticsearch-2.3.3/bin/ I created the following file

/Library/LaunchDaemons/org.elasticsearch.plist

<?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>Label</key>
        <string>org.elasticsearch</string>
        <key>ProgramArguments</key>
        <array>
            <string>/opt/local/elasticsearch-2.3.3/bin/elasticsearch</string>
        </array>
        <key>UserName</key>
        <string>root</string>
<!--         <key>GroupName</key>
        <string>staff</string> -->
        <key>WorkingDirectory</key>
        <string>/opt/local/elasticsearch-2.3.3/bin</string>
        <key>KeepAlive</key>
        <true/>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

I then issue the following command:

sudo launchctl load -w /Library/LaunchDaemons/org.elasticsearch.plist

When I inquire with sudo launchctl list | grep elastic it returns org.elasticsearch. But when I check if elasticsearch is running with ps ax | grep elastic it is not running.

Is there something wrong with my .plist file?

Best Answer

Elasticsearch mustn't be run as root. So remove the plist from the launchd database, move the plist to /Library/LaunchAgents (or ~/Library/LaunchAgents), create /opt/local/var and modify it slightly:

<?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>EnvironmentVariables</key>
    <dict>
        <key>VAR</key>
        <string>VAL</string>
    </dict>
    <key>KeepAlive</key>
    <false/>
    <key>Label</key>
    <string>org.elasticsearch</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/local/elasticsearch-2.3.3/bin/elasticsearch</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/org.elasticsearch.stderr</string>
    <key>StandardOutPath</key>
    <string>/tmp/org.elasticsearch.stdout</string>
    <key>WorkingDirectory</key>
    <string>/opt/local/var</string>
</dict>
</plist>

Then check /opt/local/elasticsearch-2.3.3 and its subdirs for proper permissions, add EnvironmentVariables if necessary (or remove the key & dict) and load the plist.

If everything works properly you may remove StandardErrorPath and StandardOutPath and their strings.


You may also run it as daemon but with a different user then (i.e. your user name) - depends on your needs/environment.