2014-11-06 40 views
1

我想设置永远监视器。当我从它记录“app.js已经经过3点开始退出”,但它仍然运行在命令行启动我的应用程序不过哪里放置永久监视器代码?

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。这个代码应放在哪个文件中?我是否错过了永久监视器的使用方法?

回答

5

下面是永远显示器的工作原理

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(); 


app.js

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


从CLI 现在,通过键入以下命令启动您的应用程序
节点app_fm

+0

如果我想的是,脚本启动本身“永远”,我应该只是增加最多的数量到999999或有作为永远纪念这一个标志? – sanyooh 2014-11-07 09:22:20

+3

永久监视通常不是一个好主意,无限期地重新启动您的应用程序。大多数时候你的应用程序崩溃了,这将是由于代码中的错误,如果你重新启动,它会再次崩溃。重复这9999999次将会令人沮丧。我使用永久监视器进行开发,即每次代码库更改时重新启动应用程序,而不是用于生产。如果您想要解决方案在生产环境中重新启动您的应用程序,您应该使用新贵。这里是我写的一个教程来完成这个任务http://handyjs.org/article/the-kick-ass-guide-to-deploying-nodejs-web-apps-in-production – takinola 2014-11-07 19:25:32

0

老实说,我只是使用forever而不是两个都与forever-monitor(尽管我知道它在永远的文档中谈论它)。我创建了一个名为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); 
});