How do I run a node.js app as a background service?

前端 未结 26 1387
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 05:28

Since this post has gotten a lot of attention over the years, I\'ve listed the top solutions per platform at the bottom of this post.


Original post

26条回答
  •  耶瑟儿~
    2020-11-22 06:09

    If you simply want to run the script uninterrupted until it completes you can use nohup as already mentioned in the answers here. However, none of the answers provide a full command that also logs stdin and stdout.

    nohup node index.js >> app.log 2>&1 &
    
    • The >> means append to app.log.
    • 2>&1 makes sure that errors are also send to stdout and added to the app.log.
    • The ending & makes sure your current terminal is disconnected from command so you can continue working.

    If you want to run a node server (or something that should start back up when the server restarts) you should use systemd / systemctl.

提交回复
热议问题