node.js Forever + Nodemon 一起运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16369018/
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:24:45  来源:igfitidea点击:

Forever + Nodemon running together

node.jsforevernodemon

提问by Patrik Fuhrmann

Is there any way to have both of this packages running together?

有没有办法让这两个包一起运行?

So basically I want to have best from both worlds. Running server automatically (and restarting when there is an error) and also automatic updates when there is .js file change happening.

所以基本上我想要两全其美。自动运行服务器(并在出现错误时重新启动)并在发生 .js 文件更改时自动更新。

回答by Anton Borodatov

You should run something like this

你应该运行这样的东西

forever start -c nodemon app.coffee

回答by Jubair

Toxa was on the right track, the issue that cfogelberg raisedis valid, but to avoid that issue you can do the following:

Toxa 走在正确的轨道上,cfogelberg 提出的问题是有效的,但要避免该问题,您可以执行以下操作:

forever -c "nodemon --exitcrash" app.js

this makes sure nodemonactually exits (rather than giving you the "app crashed" message) and then foreverpicks it up again.

这可以确保nodemon实际退出(而不是向您提供“应用程序崩溃”消息),然后forever再次拿起它。

In forever --helpthis -cspecifies a command to run otherwise it defaults node. Without -c results in the error that is mention in the comments to this answer.

forever --help-c指定要运行的命令,否则默认为节点。没有 -c 会导致此答案的评论中提到的错误。

回答by mik01aj

There is an entry about it in the nodemonFAQ:

nodemonFAQ中有一个关于它的条目:

If you're using nodemon with forever(perhaps in a production environment), you can combine the two together. This way if the script crashes, forever restarts the script, and if there are file changes, nodemon restarts your script. For more detail, see issue 30.

To achieve this you need to add the following on the call to forever:

  • Use forever's -c nodemonoption to tell forever to run nodemoninstead of node.
  • Include the nodemon --exitcrashflag to ensure nodemon exits if the script crashes (or exits unexpectedly).
  • Tell forever to use SIGTERMinstead of SIGKILLwhen requesting nodemon to stop. This ensures that nodemon can stop the watched node process cleanly.
  • Optionally add the --uidparameter, adding a unique name for your process. In the example, the uid is set to foo.

bash forever start --uid foo --killSignal=SIGTERM -c nodemon --exitcrash server.js

To test this, you can kill the server.js process and forever will restart it. If you touch server.jsnodemon will restart it.

To stop the process monitored by forever and nodemon, simply call the following, using the uidwe assigned above (foo):

bash forever stop foo

This will stop both nodemon and the node process it was monitoring.

Note that I would notrecommend using nodemon in a production environment - but that's because I wouldn't want it restart without my explicit instruction.

如果您使用的是永远的nodemon (可能在生产环境中),您可以将两者结合在一起。这样,如果脚本崩溃,永远重新启动脚本,如果有文件更改,nodemon 会重新启动您的脚本。有关更多详细信息,请参阅第 30 期

为此,您需要在调用中添加以下内容 forever

  • 使用永远的-c nodemon选项告诉永远运行nodemon而不是node.
  • 包括 nodemon--exitcrash标志以确保 nodemon 在脚本崩溃(或意外退出)时退出。
  • 告诉永远使用SIGTERM而不是SIGKILL在请求 nodemon 停止时使用。这确保 nodemon 可以干净地停止被监视的节点进程。
  • (可选)添加--uid参数,为您的流程添加唯一名称。在示例中,uid 设置为foo

bash forever start --uid foo --killSignal=SIGTERM -c nodemon --exitcrash server.js

要对此进行测试,您可以终止 server.js 进程并永远重新启动它。如果你touch server.jsnodemon 会重启它。

要停止永久和 nodemon 监视的进程,只需使用uid我们在上面分配的 ( foo)调用以下命令:

bash forever stop foo

这将停止 nodemon 和它正在监视的节点进程。

请注意,我建议在生产环境中使用 nodemon - 但那是因为我不希望它在没有我明确指示的情况下重新启动。

回答by cfogelberg

I have not found a way of getting both packages running together. I tried to do @toxa's technique, but when my node.js app threw an exception nodemon would not automatically restart it, instead outputting an error message to the forever log:

我还没有找到让两个包一起运行的方法。我尝试使用@toxa 的技术,但是当我的 node.js 应用程序抛出异常时,nodemon 不会自动重新启动它,而是将错误消息输出到永久日志:

nodemon] app crashed - waiting for file changes before starting...

However, forever has a -woption and the following command is effectively the same as if I'm running nodemon and forever together:

但是,永远有一个-w选项,以下命令实际上与我正在运行 nodemon 并永远在一起一样:

forever start -w my-app.js

The downside of forever -wversus nodemon: foreverdoes not have a --delay option, so my server gets restarted once for each file that is changed.

forever -wvs nodemon:的缺点是forever没有 --delay 选项,所以我的服务器会为每个更改的文件重新启动一次。

回答by bcbrian

I prefer a combo of what Toxa and Jubair suggest.

我更喜欢 Toxa 和 Jubair 建议的组合。

forever start -c nodemon app.coffee --exitcrash

回答by Karl Morrison

If you need to pass arguments:

如果您需要传递参数:

forever start -c "nodemon --harmony" app.js --exitcrash

回答by Julia Anne Jacobs

I'm using forever-service. . .

我正在使用永远服务。. .

This is what worked for me. It does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):

这对我有用。它执行以下操作:每次修改应用程序 dist/assets 文件夹中的 json 或 raml 文件时,等待 10 秒,然后重新启动节点应用程序(server.js 脚本):

$ forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"

Then I can run:

然后我可以运行:

$ service raml start|stop|restart|status

I can also have the service start on server reboot with the chkconfig utility:

我还可以使用 chkconfig 实用程序在服务器重新启动时启动服务:

$ chkconfig --add raml
$ chkconfig raml on

回答by Jasmeet Singh Chhabra

when using in the package.json use single quotes to make nodemon --existcrash as a single argument. "start": "forever -c 'nodemon --exitcrash' server.js"

在 package.json 中使用时,使用单引号将 nodemon --existcrash 作为单个参数。 "start": "forever -c 'nodemon --exitcrash' server.js"

Output: app_1 | [nodemon] app crashed app_1 | error: Forever detected script exited with code: 1 app_1 | error: Script restart attempt #1 app_1 | [nodemon] 1.19.4 app_1 | [nodemon] to restart at any time, enterrsapp_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] startingnode /app/server.js` app_1 | app is running on port 3000

输出: app_1 | [nodemon] app crashed app_1 | error: Forever detected script exited with code: 1 app_1 | error: Script restart attempt #1 app_1 | [nodemon] 1.19.4 app_1 | [nodemon] to restart at any time, enterrs app_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] startingnode /app/server.js` app_1 | 应用程序在端口 3000 上运行

`

`