Node.js:优雅的重启和服务器正常运行时间,怎么样? [英] Node.js : graceful restarts and server uptime, howto?

查看:169
本文介绍了Node.js:优雅的重启和服务器正常运行时间,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Node.js Socket.io 进行实时应用,我就是我希望从本地测试阶段将其与一些用户一起测试。麻烦的是,如果我关闭ssh会话到我的服务器,它也会停止服务器我开始使用节点app.js

I've been working on a realtime app using Node.js and Socket.io, and I'd like to take it from the local testing stage to testing it out with some of our users. The trouble is, if I close the ssh session to my server it also stops the server I started using node app.js

我考虑过使用 nohup,但我偶尔会遇到分段错误或其他随机错误导致服务器崩溃的问题。我需要1)知道什么时候(并且希望为什么)我的服务器崩溃所以我可以努力改进它以减少崩溃并确保重新启动它。另外,我不能每周7天每天24小时醒来自己重启服务器,所以有一些守护进程会很可爱。

I thought about using nohup, but I occasionally get segmentation faults or other random errors that bring down the server. I need to 1) know when (and hopefully why) my server crashed so I can both work on improving it to crash less and make sure to get it restarted. Additionally, I can't be awake 24/7 to restart the server myself, so having some sort of daemon would be lovely.

我发现永远可通过npm获得,但它与 0.8.x 而且我正在运行 0.9.1 ,而且它似乎不能很好地维护。

I found forever available via npm, but it isn't compatible with versions of Node more recent than 0.8.x and I'm running 0.9.1, and it doesn't seem like it gets maintained very well.

我也偶然发现了发行版上来,但是使用它们建立一个体面的应用程序的文档和示例似乎缺乏。

I also stumbled past distribution and up, but documentation and examples of building a decent app using them seem lacking.

然后使用集群 os 模块。 https://stackoverflow.com/a/10997753/1883464

但Node将群集列为实验性 http://nodejs.org/api/cluster.html

But Node lists cluster as experimental http://nodejs.org/api/cluster.html

由于缺乏在实际生产环境中保持节点服务器正常运行(以及更新代码!)的答案,示例和讨论,我开始我觉得自己是第一个想要将他们的应用程序部署到部署的人;)不知何故,我确信我找不到这个问题的常见答案。

With the lack of answers, examples, and discussion on keeping Node servers up and running (and updating their code!) in a real production environment I'm starting to feel like I'm the first person to want to take their app to deployment ;) Somehow I'm sure there are some common answers to this problem I haven't found.

推荐答案

OS提供的init系统比任何Node.js模块都提供了许多优势。
连接到init系统是确保应用程序在重启后自动启动的唯一方法。

The OS provided init system provides many advantages over any Node.js module. Hooking into the init system is the only way to ensure your app starts up automatically after a reboot.

我们使用Ubuntu和Upstart取得了很大的成功。
Upstart将在崩溃时重启您的应用程序,并可在运行您的进程之前设置用户ID /组。 不以root身份运行

We use Ubuntu and Upstart with a lot of success. Upstart will restart your application on crash, and can set the user-id/group before running your process. Do Not Run as Root.

写Upstart文件有点痛苦,我们使用 Node Foreman 从我们的应用程序中自动生成并导出一组upstart文件 Procfile

Writing Upstart files is a bit of a pain, we use Node Foreman to automatically generate and export a set of upstart files from our applications Procfile.

npm i -g foreman
cd MY_APP
nf export -o /etc/init

这会将一组新贵文件放入 / etc / init 中可以使用 sudo start foreman sudo stop foreman 来启动和停止。

This will place a set of upstart files into /etc/init that can be started and stopped with sudo start foreman and sudo stop foreman.

Upstart不是这里唯一的解决方案,上面的内容可以根据需要进行调整以适应其他操作系统。在Redhat上,我建议查看 systemd 。我不建议使用Mac OSX进行生产,但在紧要关头它有 launchd

Upstart is not the only solution here, and the above can be adapted to suit other OSes as needed. On Redhat I recommend looking at systemd. I wouldn't recomment using Mac OSX for production, but in a pinch it has launchd.

这篇关于Node.js:优雅的重启和服务器正常运行时间,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆