A simple CLI tool for ensuring that a given script runs continuously (i.e. forever).
curl http://npmjs.org/install.sh | sh
[sudo] npm install forever
There are two distinct ways to use forever: through the command line interface, or by requiring the forever module in your own code.
You can use forever to run any kind of script continuously (whether it is written in node.js or not). The usage options are simple:
usage: forever [start | stop | stopall | list | cleanlogs] [options] SCRIPT [script options] options: start start SCRIPT as a daemon stop stop the daemon SCRIPT stopall stop all running forever scripts list list all running forever scripts cleanlogs [CAREFUL] Deletes all historical forever log files -m MAX Only run the specified script MAX times -l LOGFILE Logs the forever output to LOGFILE -o OUTFILE Logs stdout from child script to OUTFILE -e ERRFILE Logs stderr from child script to ERRFILE -p PATH Base path for all forever related files (pid files, etc.) -s, --silent Run the child script silencing stdout and stderr -h, --help You're staring at it [Long Running Process] The forever process will continue to run outputting log messages to the console. ex. forever -o out.log -e err.log my-script.js [Daemon] The forever process will run as a daemon which will make the target process start in the background. This is extremely useful for remote starting simple node.js scripts without using nohup. It is recommended to run start with -o -l, & -e. ex. forever start -l forever.log -o out.log -e err.log my-daemon.js forever stop my-daemon.js
There are several samples designed to test the fault tolerance of forever. Here's a simple example:
forever samples/error-on-timer.js -m 5
You can also use forever from inside your own node.js code.
var forever = require('forever'); var child = new (forever.Forever)('your-filename.js', { max: 3, silent: true, options: [] }); child.on('exit', this.callback); child.start();
You can spawn non-node processes too. Either set the command
key in the
options
hash or pass in an Array
in place of the file
argument like this:
var forever = require('forever'); var child = forever.start([ 'perl', '-le', 'print "moo"' ], { max : 1, silent : true });
There are several options that you should be aware of when using forever:
{ 'max': 10, // Sets the maximum number of times a given script should run 'forever': true, // Indicates that this script should run forever 'silent': true, // Silences the output from stdout and stderr in the parent process 'logFile': 'path/to/file', // Path to log output from forever process (when in daemon) 'pidFile': 'path/to/file', // Path to put pid information for the process(es) started 'outFile': 'path/to/file', // Path to log output from child stdout 'errFile': 'path/to/file', // Path to log output from child stderr 'command': 'perl', // Binary to run (default: 'node') 'options': ['foo','bar'], // Additional arguments to pass to the script }
Each forever object is an instance of the node.js core EventEmitter. There are several core events that you can listen for:
- error [err]: Raised when an error occurs
- stop [process]: Raised when the target script is stopped by the user
- save [path, data]: Raised when the target Forever object persists the pid information to disk.
- restart [forever]: Raised each time the target script is restarted
- exit [forever]: Raised when the call to forever.run() completes
- stdout [data]: Raised when data is received from the child process' stdout
- stderr [data]: Raised when data is received from the child process' stderr
In addition to using a Forever object, the forever module also exposes some useful methods. Each method returns an instance of an EventEmitter which emits when complete. See the forever binary script for sample usage.
Sets the specified configuration (config) for the forever module. In addition to the callback, this method also returns an event emitter which will raise the 'load' event when complete. There are two important options:
- root: Directory to put all default forever log files
- pidPath: Directory to put all forever *.pid files
Starts a script with forever.
Starts a script with forever as a daemon. WARNING: Will daemonize the current process.
Stops the forever daemon script at the specified index. These indices are the same as those returned by forever.list(). This method returns an EventEmitter that raises the 'stop' event when complete.
Stops all forever scripts currently running. This method returns an EventEmitter that raises the 'stopAll' event when complete.
Returns a list of metadata objects about each process that is being run using forever. This method is synchronous and will return the list of metadata as such.
Cleans up any extraneous forever *.pid or *.fvr files that are on the target system. This method returns an EventEmitter that raises the 'cleanUp' event when complete.
Removes all log files from the root forever directory that do not belong to current running forever processes.
The test coverage for 0.3.1 is currently lacking, but will be improved in 0.3.2.
vows test/*-test.js --spec