Easily transform a JS script into a start-stop-daemon script
Easily transform a JS script into a start-stop-daemon script.
npm install start-stop-daemon
//file script.js
startStopDaemon([options], function() {
//awesome code you want to daemonize
});
node script.js start
node script.js stop
node script.js restart
node script.js status
node script.js
outFile
: the file to log the daemon stdout. Default to 'out.log'
.errFile
: the file to log the daemon stderr. Default to 'err.log'
.max
: the max number of times the script should run. Default to 5
.More options documented on the forever-monitor page.
Options can also be passed from the command-line: node script.js start --outFile custom.log
(more details here).
Available events: 'error'
, 'start'
, 'stop'
, 'restart'
, 'exit'
, 'stdout'
, 'stderr'
(more details here).
Use the on
method to add a listener:
startStopDaemon(function() {
//code to daemonize
}).on(event, listener);
Note: In previous versions (<= 0.1.0), daemon's unexpected crashes were handled by the option onCrash
.
The same behavior can now be obtained by using the event 'restart'
. See the example below.
server.js
, a simple http server daemon var startStopDaemon = require('start-stop-daemon');
var http = require('http');
startStopDaemon(function() {
http.createServer(function(req, res) {
console.log(req.connection.remoteAddress + ' accessed ' + req.url);
if (req.url === '/error')
throw new Error('to crash server');
res.end('Hello world! Thanks for accessing ' + req.url);
}).listen(1095);
})
.on('restart', function() {
//use this.stdout.write to write in the stdout log file, not console.log
this.stdout.write('Restarting at ' + new Date() + '\n');
});
node server.js start
node server.js status
node server.js stop
out.log
crasher.js
, a timer daemon that crashes every second var startStopDaemon = require('start-stop-daemon');
var options = {
outFile: 'customOutFile.log',
errFile: 'customErrFile.log',
max: 3 //the script will run 3 times at most
};
startStopDaemon(options, function() {
var count = 0;
setInterval(function() {
console.log(++count * 200 + 'ms');
if (count >= 5) {
console.log('timer crashing after 1 second\n');
throw new Error('to crash timer');
}
}, 200);
});
node crasher.js start
node crasher.js status
to check that the daemon correctly exitedcustomOutFile.log
and customErrFile.log
.As of version 0.1.0, daemons are handled by the forever module.
This refactoring makes start-stop-daemon
much more robust than with my previous implementation.
Backward compatibility (versions <= 0.1.0) is ensured for the options except for the events (onStart
, onCrash
, ...)
which are now handled through an EventEmitter
with a proper on
method (more here).
Command-line options are not backward compatible since they are now merged with the forever ones (more here).
Copyright (c) 2012 Jie Meng-Gerard [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.