node.js 如何自动重启节点服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16547996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:30:12  来源:igfitidea点击:

how to automatically restart a node server?

node.jsrestartupstart

提问by transient_loop

We are finishing development of a project, the client is already using it but occasionally some errors occur - crashing the server.

我们正在完成一个项目的开发,客户端已经在使用它,但偶尔会发生一些错误 - 使服务器崩溃。

I know I could register a service as 'upstart' script on linux, in order to have my node service restart when it crashes.

我知道我可以在 linux 上将服务注册为“新贵”脚本,以便在我的节点服务崩溃时重新启动。

But our server is running other stuff, so we can't restart it. Well, actually, while writing, I realize I have two questions then:

但是我们的服务器正在运行其他东西,所以我们无法重新启动它。嗯,实际上,在写作时,我意识到我有两个问题:

  • Will 'upstart' work without having to reboot? Something is just whispering yes to me :)
  • If not, what other option would I have to 'respawn' my node server when it crashes?
  • “新贵”无需重新启动就可以工作吗?有些东西只是对我耳语是:)
  • 如果没有,当我的节点服务器崩溃时,我还有什么其他选择可以“重生”它?

回答by Daniel

Yes, upstart will restart your process without a reboot.

是的,新贵将在不重新启动的情况下重新启动您的进程。

Also, you should look into forever.

此外,你应该研究永远

回答by Toumi

PM2is a Production process manager for Node.js app.

PM2是 Node.js 应用程序的生产流程管理器。

回答by Francesco

If your focus for automatic restart is an always running application, I suggest to use a process manager. Process manager, in general, handles the node process(es if cluster enabled), and is responsible for the process/es execution. PM leans on the operative system: your node app and the OS are not so strinctly chained because the pm is in the middle.

如果自动重启的重点是始终运行的应用程序,我建议使用进程管理器。进程管理器,一般来说,处理节点进程(如果启用了集群),并负责进程/es 的执行。PM 依赖于操作系统:你的节点应用程序和操作系统没有那么严格的链接,因为 pm 在中间。

Final trick: put the process manager on upstart.

最后一个技巧:将流程管理器置于新贵之上。

Here是一个完整的性能改进路径。