简体   繁体   English

永远使用 Node.js

[英]Using forever with Node.js

I have a few, possibly trivial, questions with using forever with Node.js.我有一些关于永远使用 Node.js 的问题,可能是微不足道的。 From what I have read, forever can be used programatically and it maintains a list with all the scripts that use forever.从我读过的内容来看,forever 可以以编程方式使用,它维护一个包含所有永远使用的脚本的列表。 When that process dies, it automatically spawns a new one until it is stopped.当该进程终止时,它会自动生成一个新进程,直到它停止。

However, my question is, how does forever do this?但是,我的问题是,永远如何做到这一点? Does it add these scripts to be started on boot as well?它是否也添加了这些脚本以在启动时启动?

You can use forever programatically like this:您可以像这样以编程方式永远使用:

Using an instance of Forever inside a node.js script:在 node.js 脚本中使用 Forever 实例:

var forever = require('forever-monitor');

  var child = new (forever.Monitor)('your-filename.js', {
    max: 3,
    silent: true,
    options: []
  });

  child.on('exit', function () {
    console.log('your-filename.js has exited after 3 restarts');
  });

  child.start();

You should take a minute and read over the options available in the excellent documentation for Forever in the README.md您应该花一点时间阅读README.md中 Forever 的优秀文档中提供的选项

You have a number of events that can be listened for in Forever as well:您还有许多可以在 Forever 中监听的事件:

  • error [err]: Raised when an error occurs error [err]:发生错误时引发
  • start [process, fvrFile, data]: Raise when the target script is first started. start [process, fvrFile, data]:首次启动目标脚本时引发。
  • stop [process]: Raised when the target script is stopped by the user stop [process]:当目标脚本被用户停止时引发
  • save [path, data]: Raised when the target Monitor saves the pid information to disk. save [path, data]:当目标 Monitor 将 pid 信息保存到磁盘时引发。
  • restart [forever]: Raised each time the target script is restarted restart [forever]:每次重新启动目标脚本时引发
  • exit [forever]: Raised when the target script actually exits (permenantly). exit [forever]:在目标脚本实际退出(永久)时引发。
  • stdout [data]: Raised when data is received from the child process' stdout stdout [data]:从子进程的 stdout 接收到数据时引发
  • stderr [data]: Raised when data is received from the child process' stderr stderr [data]:从子进程的 stderr 接收到数据时引发

It does this by attaching event listeners to the script you're trying to run and handling them in a graceful manner.它通过将事件侦听器附加到您尝试运行的脚本并以优雅的方式处理它们来实现这一点。

The code is pretty well documented if you want to take a look at exactly how it does it.如果您想看看它是如何做到的,那么该代码的文档就很好了。

You should also read this excellent tutorial on how to keep a process running forever.您还应该阅读这篇关于如何保持进程永远运行的优秀教程

As for the second question: No, it does not add it to start at boot.至于第二个问题:不,它不会添加它以在启动时启动。 For that, you'd need to add it as an upstart job or use something like Monit to monitor and start it.为此,您需要将其添加为新贵工作或使用Monit之类的东西来监视和启动它。 For that, you should take a look at Deploying Node.js with Upstart and Monit .为此,您应该查看Deploying Node.js with Upstart 和 Monit It's a great tutorial.这是一个很棒的教程。

This is an old post, but I stumbled across this on Google - its a little out of date, as forever branched the command line version from the programatic version.这是一篇旧帖子,但我在 Google 上偶然发现了这个——它有点过时了,因为命令行版本永远从程序版本中分支出来。 You need to use forever-monitor instead of forever .您需要使用forever-monitor而不是forever The example code should now be;示例代码现在应该是;

 var forever = require('forever-monitor');

  var child = new (forever.Monitor)('your-filename.js', {
    max: 3,
    silent: true,
    options: []
  });

  child.on('exit', function () {
    console.log('your-filename.js has exited after 3 restarts');
  });

  child.start();

I tried to suggest an edit to the original answer, but the powers that be rejected it.我试图建议对原始答案进行编辑,但被拒绝的权力。 Figured I could spare some others the time it took me to figure out why the example code doesn't work:-)想我可以节省一些其他人的时间来弄清楚为什么示例代码不起作用:-)

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

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