19

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?

1
  • 3
    Thank you @Dan Davies Brackett Commented Jun 29, 2011 at 18:26

2 Answers 2

29

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The second half of your answer is exactly what I was looking for. I wasn't sure if that's what I had to do, so thanks for the help.
Glad I could help. Definitely read that tutorial on deploying node. I've used that method with a few tweaks very successfully in several past projects.
I had already come across the forever page on github, the upstart and monit tutorial, I will indeed read, now. Thanks
@slickplaid Can you plz explain what the code does? because I want to restart my node.js script/server programmatically each time server crashes or the site goes down. How can I handle that programmatically to auto restart my script if it dies or if the site is not giving 200 OK ?
5

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 :-)

2 Comments

Can you explain what the code does? because I want to restart my node.js script/server programmatically each time server crashes or the site goes down. How can I handle that programmatically to auto restart my script if it dies ?
@Faizan I'd recommend using forever from the command line instead of in code. If the server reboots, you can add a '@reboot' entry into a crontab to fire it up again. Also pm2 is worth a look too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.