把你的nodejs脚本交给forever吧,它可以让你的nodejs起死回生!
文章来源:http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever
One of the great benefits of using node.js is the reduction in dependencies needed to run a production web application. By using the 'http' module we can run a stand-alone web server in node.js without the need for a separate server like Apache or nginx. The caveat of not having to use these servers is that their concerns are now the concerns of the node.js application developer. The concern that we will discuss in this article is that of fault tolerance, or how to automatically restart your server when it crashes or enters an invalid state.
Before we dive into how to use Forever, lets setup the 'hello world' web server that we will later run with Forever.
The above code starts a web server that will respond to all requests with 'hello, i know nodejitsu'. Starting this server is easy:
Running in the background
By simply running our server script with the 'node' command the server will start as a long running process (i.e. it will block the current shell until it crashes or is forced to exit with Ctrl-C). But what if we want to run this server in the background? The traditional approach is to run the command using nohup:
This will start our server process in the background and append all output to 'output.log'. Another approach is to start the script as a daemon using a library like daemon.node. I won't go into the details of using daemon.node, but the documentation on the Github project page. These two approaches solve the background problem, but what if we have a buggy server like the one below:
This is a contrived example that will start a web server, then throw an unhandled exception after five seconds. If we were to run this script, either in the background or a long running process, it will not automatically restart when it crashes.
Keeping it running with Forever
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. It's worth mentioning that there are other tools written to accomplish this task in a more generic way for any program or programming language:
- Monit: http://mmonit.com/monit/
- Upstart: http://upstart.ubuntu.com/
- Daemontools: http://cr.yp.to/daemontools.html
- Launchtool: http://people.debian.org/~enrico/launchtool.html
Currently Forever is for running node.js scripts only, but this is something that may be changed in the future. Honestly, it's a one line fix here, but I'm not sure if users want to put 'node' in-front of every command. Forever is used in production at Nodejitsu and we can confirm that since we deployed it on our servers a month ago, we have not needed to touch it at all. Forever exposes its functionality via a command line interface available after installation via npm:
The usage options for Forever expose four simple command line tasks: start, stop, stopall, list:
You can also use Forever as a long running process by omitting the 'start' option. With these tasks available, starting a nodejs script with Forever is simple:
The first command starts the 'simple-server.js' script in the background using daemon.node and returns control to the current shell process. The second command lists all processes running with Forever. The IDs after the script name are the process IDs of the target script and the forever daemon watching that script respectively. We can confirm this by looking at the process list ourselves:
Stopping and restarting with Forever
To illustrate that Forever will automatically restart a child process that exits, lets kill the process ourselves:
As you can see the process ID of the target script 'simple-server.js' has changed from 24597 to 24611 indicating that a new process has been started by Forever. So our target script will run continuously, but how do we stop it? Stopping a process with Forever is simple from the command line. We simply need to pass the index for that process show from 'forever list':
Additional Forever options
There are some default options and configuration conventions that you should be aware of when using Forever:
- Forever keeps track of running processes in *.fvr files that are placed in /tmp/forever/pids
- Each Forever process will generate a unique log file placed in /tmp/forever/*.log
- Unless otherwise specified, the output of the child process' stdout and stderr will be written to the above log file.