OSX launchd plist for node forever process

lostintranslation picture lostintranslation · Sep 4, 2013 · Viewed 8.9k times · Source

I am trying to write a launchd.plist file for my node server. I am using forever to run my node server. I would like the server to start on boot. I would also like to wait for the mongodb launchd plist to run first.

I installed mongobb using homebrew and it came with a launchd.plist already. I have executed the following:

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

plist for mongodb is:

<!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>homebrew.mxcl.mongodb</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/mongodb/mongod</string>
    <string>run</string>
    <string>--config</string>
    <string>/usr/local/etc/mongod.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <false/>
  <key>WorkingDirectory</key>
  <string>/usr/local</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/log/mongodb/output.log</string>
  <key>StandardOutPath</key>
  <string>/usr/local/var/log/mongodb/output.log</string>
  <key>HardResourceLimits</key>
  <dict>
    <key>NumberOfFiles</key>
    <integer>1024</integer>
  </dict>
  <key>SoftResourceLimits</key>
  <dict>
    <key>NumberOfFiles</key>
    <integer>1024</integer>
  </dict>
</dict>
</plist>

If I shutdown the computer and restart mongodb fires up as it should.

However my node server is not starting. Any ideas?

<?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>KeepAlive</key>
        <dict>
            <key>SuccessfulExit</key>
            <false/>
        </dict>
        <key>Label</key>
        <string>com.test.app</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/forever</string>
            <string>-a</string>
            <string>-l</string>
            <string>/var/log/app/app.log</string>
            <string>-e</string>
            <string>/var/log/app/app_error.log</string>
            <string>/data/server/app.js</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>3600</integer>
    </dict>
</plist>

EDIT:

writing to log file and I see this:

env: node: No such file or directory

I think this means that the node binary cannot be found. I can echo $PATH and /usr/local/bin is in my path. I can start node from the terminal. Ideas?

Answer

frnkxiao picture frnkxiao · Oct 15, 2014

Add environment Variables worked for me.

<key>EnvironmentVariables</key>
<dict>
  <key>PATH</key>
  <string>/usr/local/bin/:$PATH</string>
</dict>

You may also need to add WorkingDirectory to your node app.

<key>WorkingDirectory</key>
<string>path/to/your/node/app</string>