3

I am trying to set up forever-monitor.

I added this to my 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();

However when I start my application from the command line it logs 'app.js has exited after 3 starts' but it still runs. In which file should this code be placed? Am I missing something about the usage of forever-monitor?

sanyooh
  • 1,260
  • 2
  • 18
  • 31

2 Answers2

6

Here's how forever-monitor works

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


Now from the CLI start your app by typing
node app_fm

takinola
  • 1,643
  • 1
  • 12
  • 23
  • if I want that the script starts itself 'forever' should I just increase the number of max to 999999 or is there a flag to mark this as forever? – sanyooh Nov 07 '14 at 09:22
  • 3
    It is usually not a good idea to have forever-monitor restarting your app indefinitely. Most times your app crashes, it will be due to an error in the code and if you restart, it will just crash again. Repeating this 9999999 times will just be frustrating. I use forever-monitor for development i.e. to restart the app each time the code base changes and not for production. If you want a solution to restarting your app in production, you should use upstart. Here's a tutorial I wrote to do this http://handyjs.org/article/the-kick-ass-guide-to-deploying-nodejs-web-apps-in-production – takinola Nov 07 '14 at 19:25
0

Honestly, I just use forever and don't both with forever-monitor (though I know it talks about it in the forever docs!). I create a file called start.js and run my app with 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 );
} );
clay
  • 5,917
  • 2
  • 23
  • 21