An 1-function Node.js module to easily create native child_process.fork
start-stop-daemon scripts.
Created daemons are self-monitored and restart automatically when crashing (a custom crash handler can be attached too).
npm install start-stop-daemon
//file daemon.js
startStopDaemon([options], function() {
//awesome code you want to daemonize
});
- Start the daemon with the command
node daemon.js start
- Stop the daemon with the command
node daemon.js stop
- Restart the daemon with the command
node daemon.js restart
- Get the status of the daemon with the command
node daemon.js status
- Run the script not as a daemon with the command
node daemon.js run
daemonFile
: the file to log the daemon PIDs, startTime, etc... Default to'daemon.dmn'
.outFile
: the file to log the daemon stdout. Default to'daemon.out'
.errFile
: the file to log the daemon crashes (uncaught exceptions). Default to'daemon.err'
.maxCrash
: the maximum number of crashes by minute. Past this number, the daemon exits. Default to5
.crashTimeout
: a crash timeout before restarting (when restarting immediately, a server port can still be bound). Default to0
.onStart
: listener fired when we start the daemon. Default to displaying a start message.onStop
: listener fired when we stop the daemon. Default to displaying a stop message.onStatus
: listener fired when we get the status of the daemon. Default to displaying a status message.onCrash
: listener fired when the daemon crashes. Default to restarting the daemon.
--logAppend append to existing stdout and stderr files
--daemon <daemonFile> specify daemon file for PIDs, startTime...
--out <stdoutFile> specify stdout file
--err <stderrFile> specify stderr file
--max-crash <value> specify maximum number of crashes by minute
--crash-timeout <value> specify a crash timeout in ms before restarting
You can also pass your own arguments, they'll be transferred to the daemon process:
node daemon.js start --port 1095
In the daemon process, we'll have process.argv[3] === '--port'
and process.argv[4] === '1095'
.
Exit daemon:
this.crashExit();
Restart the daemon with an option to append stdout/stderr to the previous stdout/stderr files
this.crashRestart([logAppend]); //logAppend default to true
var startStopDaemon = require('..');
var http = require('http');
var HELLO_WORLD = [];
startStopDaemon(function() {
http.createServer(function(req, res) {
console.log(req.connection.remoteAddress + ' accessed ' + req.url);
HELLO_WORLD.push('Hello world! Welcome to our fantastic page ' + req.url);
res.end(HELLO_WORLD.join('\n'));
}).listen(8080);
});
Execute the command node server.js start
Play with the server in your browser on http://localhost:8080
Execute the command node server.js status
Execute the command node server.js stop
Check the stdout file server.out
.
var startStopDaemon = require('..');
var options = {
outFile: 'customOutFile.log',
errFile: 'customErrFile.log',
onCrash: function(e) {
// Logging crash in sdtout file
console.log('CRASH');
// Restart daemon if it crashes during the 3 first seconds, exit it otherwise
Date.now() - this.startTime <= 3000 ?
this.crashRestart() :
this.crashExit();
}
};
startStopDaemon(options, function() {
var count = 0;
setInterval(function() {
if (count >= 5)
throw new Error('to crash the timer');
console.log('0.' + 2 * count++ + ' second');
}, 200);
});
Execute the command node crasher.js start
Wait 4 seconds then execute the command node crasher.js status
to check that the daemon correctly exited
Check the files customOutFile.log
and customErrFile.log
.
Copyright (c) 2012 Jie Meng-Gerard contact@jie.fr
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.