Skip to content

Instantly share code, notes, and snippets.

@miguel-amaral
Created June 7, 2018 14:12
Show Gist options
  • Save miguel-amaral/2360e7fc509e09b0cbdc2c3bc00356ec to your computer and use it in GitHub Desktop.
Save miguel-amaral/2360e7fc509e09b0cbdc2c3bc00356ec to your computer and use it in GitHub Desktop.
Example of dealing with the close of a nodejs process
process.stdin.resume();//so the program will not close instantly
function exitHandler(options, err) {
if (options.cleanup) console.log('clean');
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment