Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using forever with Node.js

Tags:

node.js

I have a few, possibly trivial, questions with using forever with Node.js. From what I have read, forever can be used programatically and it maintains a list with all the scripts that use 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?

like image 600
cowboybebop Avatar asked Jun 29 '11 17:06

cowboybebop


People also ask

Why do you use forever with node js?

The purpose of Forever is to keep a child process (such as your node. js web server) running continuously and automatically restart it when it exits unexpectedly.

What is the use of Forever npm?

Forever is an npm module that ensures a Node. js script continuously runs in the background on the server. It's a helpful CLI tool for the production environment because it helps manage the Node applications and their processes.

What is forever command?

forever is a command-line utility for Node applications written entirely in JavaScript. It is meant to simplify your life in a production environment by managing (starting, stopping, restarting, etc) Node processes and their configurations.


2 Answers

You can use forever programatically like this:

Using an instance of Forever inside a node.js script:

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

You have a number of events that can be listened for in Forever as well:

  • error [err]: Raised when an error occurs
  • start [process, fvrFile, data]: Raise when the target script is first started.
  • stop [process]: Raised when the target script is stopped by the user
  • save [path, data]: Raised when the target Monitor saves the pid information to disk.
  • restart [forever]: Raised each time the target script is restarted
  • exit [forever]: Raised when the target script actually exits (permenantly).
  • stdout [data]: Raised when data is received from the child process' stdout
  • stderr [data]: Raised when data is received from the child process' 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. For that, you should take a look at Deploying Node.js with Upstart and Monit. It's a great tutorial.

like image 97
slickplaid Avatar answered Oct 02 '22 15:10

slickplaid


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. You need to use forever-monitor instead of 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 :-)

like image 26
Mike P Avatar answered Oct 02 '22 15:10

Mike P