gpt4 book ai didi

node.js - Node 服务器在永远停止后运行

转载 作者:IT老高 更新时间:2023-10-28 23:08:36 24 4
gpt4 key购买 nike

我有一个 Node 应用程序作为守护进程在服务器上永远运行。在其中一个更新之后,我试图停止它(稍后重新启动)。但令我惊讶的是 forever stopall 什么也没做 No forever processes running 被返回。 Forever list 已返回相同的值。我也用 sudo 试过这两个命令。

问题是我可以清楚地看到 Node 仍在运行(我的应用程序运行良好)。知道出了什么问题吗?

P.S.我需要尽快更新,所以我只是重新启动了服务器。但我还是对这种情况很好奇。

P.S.S 后输入 ps aux | grep app.js

ubuntu    1320  0.0  2.2 663040 23232 ?        Ssl  Sep12   0:00 /usr/bin/nodejs /usr/lib/node_modules/forever/bin/monitor node_app/app.js
ubuntu 1322 0.0 6.9 992564 70792 ? Sl Sep12 0:31 /usr/bin/nodejs /var/www/node_app/app.js
root 9739 0.0 0.0 10468 936 pts/0 S+ 11:09 0:00 grep --color=auto app.js

为什么会这样?我在亚马逊 AWS 上运行 node app.js。

最佳答案

解决方案 1 - 使用 --uid

I´ve managed how to mark your apps in forever using a UID, and then not using forever stopall. This is a cleaner way, and will make forever to kill all the processes depending on the script.

You just need to add the --uid "scriptID" parameter, and then all the processes depending on it, will be controlled together.

To start a new daemon: forever start --uid "demo" --sourceDir /home/procdir -l /home/log/logfile -o /home/log/outputfile -a -d -v taskName

To Stop a daemon: forever stop -uid "demo"

-bash-4.1$ forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [1] Test /usr/bin/node grunt serve:test 18217 18224 /home/admin/logs/test/forever.log 59:20:21:10.512
data: [2] Dev /usr/bin/node grunt serve:dev 18347 18354 /home/admin/logs/dev/forever.log 59:20:19:56.87
data: [3] Prod /usr/bin/node grunt serve:prod 20411 20418 /home/admin/logs/prod/forever.log 59:18:58:28.697

Anyway, you can also run (hands way) this command to kill the processes: First, kill all forever tasks (this will prevent forever to run the task again when killing it):

 forever list | grep your_app | `awk '/\[0\]/{print "forever stop "$8}'` 

After that, when forever is killed, then now it´s time to kill your node_app

ps -efa | grep  node | grep your_app | `awk '{ print "kill "$2}'` 

I strongly recommend you to not using kill. forever --uid will surely be the best solution.

希望此解决方案对您有所帮助!

解决方案 2 - 永远 --uid 已弃用

As forever --uid was deprecated, I´ve tried to find the way of managing multiple apps by name (without using the --uid deprecated method). It seems using config files is the solution. As read in forever docs uid and id parameters are still in use in config files. After some tests, I´ve managed that id is the right parameter:

例子:

-bash-4.1$ pwd
/jome/myuser/app
-bash-4.1$ forever list
info: No forever processes running
-bash-4.1$ forever start ./forever/development.json
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: index.js

-bash-4.1$ forever stop app4U
info: Forever stopped process:
uid command script forever pid id logfile uptime
[0] BaoO /usr/local/bin/node index.js 41196 41197 app4U /Users/me/.forever/BaoO.log 0:0:0:5.995

forever/development.json

{
// Comments are supported
"id": "app4U",
"append": true,
"watch": true,
"script": "index.js",
"sourceDir": "/jome/myuser/app"
}

index.js

a=0;
while (a<10) {
a+=1
a-=1
}

关于node.js - Node 服务器在永远停止后运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25806137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com