Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 永久监控代码在哪里?_Node.js_Forever - Fatal编程技术网

Node.js 永久监控代码在哪里?

Node.js 永久监控代码在哪里?,node.js,forever,Node.js,Forever,我正在尝试设置永久监视器 我在app.js中添加了以下内容: var forever = require('forever-monitor'); var child = new(forever.Monitor)('app.js', { max: 3, silent: true, options: [] }); child.on('exit', function() { console.log('app.js has exited after 3 restart

我正在尝试设置永久监视器

我在app.js中添加了以下内容:

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

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

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

child.start();

但是,当我从命令行启动应用程序时,它会记录“app.js已在3次启动后退出”,但它仍在运行。该代码应放在哪个文件中?我是否遗漏了永久监视器的使用方法?

老实说,我只使用了
永久监视器
,并没有同时使用
永久监视器
(尽管我知道它在永久文档中提到过!)。我创建了一个名为
start.js
的文件,并使用
node start.js
运行我的应用程序

'use strict';
var forever = require('forever');
var child = new (forever.Monitor )('app.js', {
  //options : options
} );

//These events not required, but I like to hear about it.
child.on( "exit", function() {
  console.log( 'app.js has exited!' );
} );
child.on( "restart", function() {
  console.log( 'app.js has restarted.' );
} );
child.on( 'watch:restart', function( info ) {
  console.error( 'Restarting script because ' + info.file + ' changed' );
} );

//These lines actually kicks things off
child.start();
forever.startServer( child );

//You can catch other signals too
process.on( 'SIGINT', function() {
  console.log( "\nGracefully shutting down \'node forever\' from SIGINT (Ctrl-C)" );
  // some other closing procedures go here
  process.exit();
} );

process.on( 'exit', function() {
  console.log( 'About to exit \'node forever\' process.' );
} );

//Sometimes it helps...
process.on( 'uncaughtException', function( err ) {
  console.log( 'Caught exception in \'node forever\': ' + err );
} );

下面是永久监视器的工作原理

app_fm.js

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

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

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

child.start();
// put in all your great nodejs app code
console.log('node app is now running');

app.js

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

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

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

child.start();
// put in all your great nodejs app code
console.log('node app is now running');

现在,在CLI中,通过键入

node app_fm

如果我希望脚本自动“永久”启动,我是否应该将max的数量增加到999999,或者是否有标志将其标记为永久?让永久监视器无限期地重新启动应用程序通常不是一个好主意。大多数情况下,应用程序崩溃都是由于代码中的错误,如果重新启动,它将再次崩溃。重复这个9999999次只会令人沮丧。我使用forever monitor进行开发,即每次代码库更改时重新启动应用程序,而不是用于生产。如果您想要在生产环境中重新启动应用程序的解决方案,您应该使用upstart。这是我写的一个教程