IMPORTANT: This is the repack of the original version with updated dependencies and fixed tests
forever-monitor
The core monitoring functionality of forever without the CLI
Usage
You can also use forever from inside your own Node.js code:
var forever = require('forever-monitor');
var child = new (forever.Monitor)('your-filename.js', {
max: 3,
silent: true,
args: []
});
child.on('exit', function () {
console.log('your-filename.js has exited after 3 restarts');
});
child.start();
Spawning a non-node process
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-monitor');
var child = forever.start([ 'perl', '-le', 'print "moo"' ], {
max : 1,
silent : true
});
Options available when using Forever in node.js
There are several options that you should be aware of when using forever. Most of this configuration is optional.
{
'silent': false,
'uid': 'your-UID',
'pidFile': 'path/to/a.pid',
'max': 10,
'killTree': true,
'minUptime': 2000,
'spinSleepTime': 1000,
'command': 'perl',
'args': ['foo','bar'],
'sourceDir': 'script/path',
'watch': true,
'watchIgnoreDotFiles': null,
'watchIgnorePatterns': null,
'watchDirectory': null,
'spawnWith': {
customFds: [-1, -1, -1],
setsid: false,
uid: 0,
gid: 0,
shell: false
},
'env': { 'ADDITIONAL': 'CHILD ENV VARS' },
'cwd': '/path/to/child/working/directory',
'logFile': 'path/to/file',
'outFile': 'path/to/file',
'errFile': 'path/to/file',
'parser': function (command, args) {
return {
command: command,
args: args
};
}
}
Events available when using an instance of Forever in node.js
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
- start [process, data]: Raised when the target script is first started.
- stop [process]: Raised when the target script is stopped by the user
- restart [forever]: Raised each time the target script is restarted
- exit [forever]: Raised when the target script actually exits (permanently).
- stdout [data]: Raised when data is received from the child process' stdout
- stderr [data]: Raised when data is received from the child process' stderr
Typical console output
When running the forever CLI tool, it produces debug outputs about which files have changed / how processes exited / etc. To get a similar behaviour with forever-monitor
, add the following event listeners:
const child = new (forever.Monitor)('your-filename.js');
child.on('watch:restart', function(info) {
console.error('Restarting script because ' + info.file + ' changed');
});
child.on('restart', function() {
console.error('Forever restarting script for ' + child.times + ' time');
});
child.on('exit:code', function(code) {
console.error('Forever detected script exited with code ' + code);
});
Installation
$ npm install forever-monitor
Run Tests
$ npm test
License: MIT