简体   繁体   English

Node.js服务器的永久进程并非一直运行

[英]Forever process for Node.js server is not running all time

I am running a forever process for Node.js server but after one day the server stops the process.My server is running on Ubuntu platform. 我正在为Node.js服务器运行一个永久进程,但是一天后服务器停止了该进程。我的服务器正在Ubuntu平台上运行。 I have done the following process: 我已经完成以下过程:

First I installed npm install forever and ran the command forever start server.js . 首先,我安装了npm install forever并运行了命令forever start server.js I need the server to run for all the time but after one day I am seeing the server stops working. 我需要服务器一直运行,但是一天后,我看到服务器停止工作。

Please help me to resolve this issue. 请帮助我解决此问题。

I would like to suggest that you try PM2 instead. 我建议您改用PM2 Here's the short tutorial I wrote about it: http://www.nikola-breznjak.com/blog/nodejs/using-pm2-to-run-your-node-js-apps-like-a-pro/ . 这是我写的简短教程: http : //www.nikola-breznjak.com/blog/nodejs/using-pm2-to-run-your-node-js-apps-like-a-pro/

edit: As per StackOverflow's policy I'm including the content from the post here also: 编辑:根据StackOverflow的政策,我还将发布中的内容包括在内:

Running your Node.js application by hand is, well, not the way we roll. 手动运行Node.js应用程序不是我们的工作方式。 Imagine restarting the app every time something happens, or god forbid application crashes in the middle of the night and you find about it only in the morning – ah the horror. 想象一下,每当发生任何事情时重新启动应用程序,或者上帝禁止应用程序在半夜崩溃,而您只能在早晨才能发现它-这真是恐怖。 PM2 solves this by: PM2通过以下方法解决了这一问题:

  • allowing you to keep applications alive forever 让您永远保持应用程序的生命
  • reloading applications without downtime 无需停机即可重新加载应用程序
  • facilitating common system admin tasks 促进常见的系统管理任务

To install PM2, run the following command: 要安装PM2,请运行以下命令:

sudo npm install pm2 -g

To start your process with PM2, run the following command (once in the root of your application): 要使用PM2启动流程,请运行以下命令(一次在应用程序的根目录中):

pm2 start server.js

As you can see from the output shown on the image below, PM2 automatically assigns an App name (based on the filename, without the .js extension) and a PM2 id. 从下图所示的输出中可以看到,PM2自动分配一个应用程序名称(基于文件名,不带.js扩展名)和一个PM2 ID。 PM2 also maintains other information, such as the PID of the process, its current status, and memory usage. PM2还维护其他信息,例如进程的PID,其当前状态和内存使用情况。

As I mentioned before, the application running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). 如前所述,如果PM2下运行的应用程序崩溃或被杀死,它将自动重新启动,但是还需要采取其他步骤来使应用程序在系统启动(启动或重新启动)时启动。 The command to do that is the following: 为此,请执行以下命令:

pm2 startup ubuntu

The output of this command will instruct you to execute an additional command which will enable the actual startup on boot or reboot. 该命令的输出将指示您执行其他命令,该命令将在引导或重新引导时启用实际启动。 In my case the note for the additional command was: 就我而言,附加命令的注释是:

sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u nikola

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM