简体   繁体   English

Forever + Nodemon 一起运行

[英]Forever + Nodemon running together

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 文件更改时自动更新。

你应该运行这样的东西

forever start -c nodemon app.coffee

Toxa was on the right track, the issue that cfogelberg raised is valid, but to avoid that issue you can do the following: Toxa 走在正确的轨道上, cfogelberg 提出的问题是有效的,但要避免该问题,您可以执行以下操作:

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

this makes sure nodemon actually exits (rather than giving you the "app crashed" message) and then forever picks it up again.这可以确保nodemon真正退出(而不是给你“应用程序崩溃”消息),然后forever再次拿起它。

In forever --help this -c specifies a command to run otherwise it defaults node.forever --help-c指定要运行的命令,否则默认节点。 Without -c results in the error that is mention in the comments to this answer.没有 -c 会导致此答案的评论中提到的错误。

There is an entry about it in the nodemon FAQ :nodemon FAQ 中有一个关于它的条目:

If you're using nodemon with forever (perhaps in a production environment), you can combine the two together.如果您使用的是永远的nodemon(可能在生产环境中),您可以将两者结合在一起。 This way if the script crashes, forever restarts the script, and if there are file changes, nodemon restarts your script.这样,如果脚本崩溃,永远重新启动脚本,如果有文件更改,nodemon 会重新启动您的脚本。 For more detail, see issue 30 .有关更多详细信息,请参阅第 30 期

To achieve this you need to add the following on the call to forever :要实现这一点,您需要在对forever的调用中添加以下内容:

  • Use forever's -c nodemon option to tell forever to run nodemon instead of node .使用永远的-c nodemon选项告诉永远运行nodemon而不是node
  • Include the nodemon --exitcrash flag to ensure nodemon exits if the script crashes (or exits unexpectedly).包括 nodemon --exitcrash标志以确保 nodemon 在脚本崩溃(或意外退出)时退出。
  • Tell forever to use SIGTERM instead of SIGKILL when requesting nodemon to stop.当请求 nodemon 停止时,告诉永远使用SIGTERM而不是SIGKILL This ensures that nodemon can stop the watched node process cleanly.这确保 nodemon 可以干净地停止被监视的节点进程。
  • Optionally add the --uid parameter, adding a unique name for your process. (可选)添加--uid参数,为您的进程添加唯一名称。 In the example, the uid is set to foo .在示例中,uid 设置为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.要对此进行测试,您可以终止 server.js 进程并永远重新启动它。 If you touch server.js nodemon will restart it.如果你touch server.js nodemon 将重新启动它。

To stop the process monitored by forever and nodemon, simply call the following, using the uid we assigned above ( foo ):要停止永久和 nodemon 监视的进程,只需使用我们在上面分配的uid ( foo ) 调用以下命令:

bash forever stop foo

This will stop both nodemon and the node process it was monitoring.这将停止 nodemon 和它正在监视的节点进程。

Note that I would not recommend using nodemon in a production environment - but that's because I wouldn't want it restart without my explicit instruction.请注意,我建议在生产环境中使用 nodemon - 但那是因为我不希望它在没有我明确指示的情况下重新启动。

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 -w option 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 -w versus nodemon : forever does not have a --delay option, so my server gets restarted once for each file that is changed. forever -wnodemon的缺点: forever没有 --delay 选项,所以我的服务器会为每个更改的文件重新启动一次。

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

forever start -c nodemon app.coffee --exitcrash

如果您需要传递参数:

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

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

when using in the package.json use single quotes to make nodemon --existcrash as a single argument.在 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, enter输出: 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, enter 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, enter 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, enter rs app_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] starting 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, enter rs app_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] starting app_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] starting app_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] starting node /app/server.js` app_1 | app_1 | [nodemon] watching dir(s): *.* app_1 | [nodemon] watching extensions: js,mjs,json app_1 | [nodemon] starting节点/app/server.js` app_1 | app is running on port 3000应用程序在端口 3000 上运行

` `

Use like this: "start": "firever -c \"nodemon --exitcrash\" <main>.js"像这样使用: "start": "firever -c \"nodemon --exitcrash\" <main>.js"

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

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