[英]How to run node js using forever without stopping?
I am running a chat server in my application using node js.我正在使用 node js 在我的应用程序中运行聊天服务器。 I have installed forever in the server.
我已经永远安装在服务器上。 Usually, I start node using forever by the following command:
通常,我通过以下命令永久使用节点:
forever start server.js &
The problem is that the javascript file stops running after some time.问题是 javascript 文件在一段时间后停止运行。 It shows the following in the terminal:
它在终端中显示以下内容:
Write failed: Broken pipe
When I login to my server once again and type the start command, the node keeps up.当我再次登录到我的服务器并键入 start 命令时,节点会跟上。 What command should I use to keep forever running even after my local session gets logged out?
即使我的本地会话被注销,我应该使用什么命令来保持永远运行?
To run a script infinitely as a background task, you need process manager tools.要将脚本作为后台任务无限运行,您需要流程管理器工具。 PM2 is my favorite process manager tool made in nodejs but can run any terminal task because it is a CLI.
PM2 是我最喜欢的用 nodejs 制作的进程管理器工具,但可以运行任何终端任务,因为它是一个 CLI。
Basically, you can install NodeJs and npm to reach pm2.基本上,你可以安装 NodeJs 和 npm 来达到 pm2。 (You can visit NodeJs.org to download the installer.)
(您可以访问NodeJs.org下载安装程序。)
You need to install the pm2 as a global module using npm install -g pm2
on your terminal您需要在终端上使用
npm install -g pm2
将npm install -g pm2
为全局模块
You can check if it is installed simply by pm2 -v
您可以通过
pm2 -v
检查它是否已安装
Then you can start your nodejs script on your terminal using pm2 start file_name.js
然后你可以使用
pm2 start file_name.js
在你的终端上启动你的 nodejs 脚本
It will create a thread in background to run your script and it will be restart forever.它将在后台创建一个线程来运行您的脚本,它将永远重新启动。
If you were doing something that takes so much time and you dont want to see the task running on the terminal you can just disable restarting by adding the parameter --no-autorestart
into the command.如果您正在做一些需要很长时间的事情并且您不想看到在终端上运行的任务,您可以通过在命令中添加参数
--no-autorestart
来禁用重新启动。 (# pm2 start file_name.js --no-autorestart
) (#
pm2 start file_name.js --no-autorestart
)
If you want to see the logs or the state of the task, you can just use pm2 status
, pm2 logs
and pm2 monit
.如果你想查看任务的日志或状态,你可以只使用
pm2 status
、 pm2 logs
和pm2 monit
。
If you want to stop the task, you can use pm2 stop task_name
如果要停止任务,可以使用
pm2 stop task_name
You can use pm2 reload all
or pm2 update
to start all the tasks back您可以使用
pm2 reload all
或pm2 update
将所有任务重新启动
You can kill the task using pm2 kill
您可以使用
pm2 kill
任务
For more information you can visit PM2 Documentation有关更多信息,您可以访问PM2 文档
Assuming you're on some Linux system, you could use screen
.假设您在某个 Linux 系统上,您可以使用
screen
。 It's incredibly simple.这非常简单。
There are many tutorials out there, just google for 'screen linux'那里有很多教程,只需在谷歌上搜索“screen linux”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.