Skip to content

Instantly share code, notes, and snippets.

@zenlor
Created February 14, 2012 11:09
Show Gist options
  • Select an option

  • Save zenlor/1825834 to your computer and use it in GitHub Desktop.

Select an option

Save zenlor/1825834 to your computer and use it in GitHub Desktop.
daemonizing wrapper for node apps
var express = require('express')
, app = module.exports = express.createServer();
// you express app resides here ...
var daemon = require('daemon')
, app = require('./app')
, db = require('mongoose');
// `QUIT` Signal handling
process.on('QUIT', function() {
console.log("SIGQUIT received! exiting");
// Close the db connection
db.close(function(e) {
if (e) console.log('ERROR closing the DB connection:\n', e);
// Delete the pid file
fs.unlink('./tmp/node.pid', function(e) {
if (e) console.log(e);
// Exit
process.exit();
});
});
});
// I Expect a $PORT environment variable
app.listen( parseInt(process.env.PORT, 10) || 3001, function(err){
if ( err ) {
throw err;
}
// Daemonize the Application once started logging to our `./log/production.log` and `./log/production.error.log`
daemon.daemonize({
stdout: './log/' +process.env.ENV+ '.log'
, stderr: './log/'+process.env.ENV +'.error.log'
}
, './tmp/node.pid'
, function (err, pid) {
if (err) {
console.log('Error starting daemon: ');
throw err;
}
console.log('Daemonized successfully with pid: ' + pid);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment