OSX launchd plist for node forever process

前端 未结 5 489
旧时难觅i
旧时难觅i 2021-02-03 12:24

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 fo

相关标签:
5条回答
  • 2021-02-03 12:55

    Add node before forever:

    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/node</string>
        <string>/usr/local/bin/forever</string>
        <string>/path/to/app.js</string>
    </array>
    
    0 讨论(0)
  • 2021-02-03 12:59

    I had this problem too, but I solved it using an Automator app that runs at startup.

    1. Open Automator and choose, New Application

    2. Insert in your workflow "Run Shell Script"

    3. Use this code in the shell script, changing the paths to your paths

    export PATH=/usr/local/bin/:$PATH
    cd /path/to/your/nodejs/app
    forever start app.js
    
    1. Go to System Preferences >> User & Groups and click on Login Items tab

    2. Add your Automator app and be happy.

    The important part of the solution is the first line of the script (adding your bin to the path). It would probably work to add a Startup Item pointed at a bash script too (and no Automator script), feel free to try!

    0 讨论(0)
  • 2021-02-03 13:03

    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>
    
    0 讨论(0)
  • 2021-02-03 13:04

    I'm not sure when node-launchd came out. However, it seems to be the more reliable solution.

    The solution in which a workflow is created and added into the login item is also well appreciated. However, the problem is that if the app is served on a server and when the server is restarted, I wonder the app will be started before the user login to the system. Didn't try, though.

    0 讨论(0)
  • 2021-02-03 13:05

    This is not an answer of the original questions, but I was looking for a simple way to start a node server (and keep it running) after reboot. I found pm2 to be a much easier to set up than the solutions for forever above.

    # install pm2
    npm install pm2 -g
    
    # start server
    pm2 start app.js
    
    # start pm2 after reboot (might need sudo)
    $ pm2 startup
    

    http://pm2.keymetrics.io/docs/usage/startup/

    0 讨论(0)
提交回复
热议问题