Skip to content

Instantly share code, notes, and snippets.

@Sv443
Created November 21, 2018 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sv443/1d56de5ff9f8c214990f78325282f824 to your computer and use it in GitHub Desktop.
Save Sv443/1d56de5ff9f8c214990f78325282f824 to your computer and use it in GitHub Desktop.
NodeJS soft shutdown
// not compatible with ES5, only ES6 and up
process.on('SIGINT', ()=>{
try {
// end your processes here, for example
// http_connection.close();
// or
// mysql_connection.end();
process.exit(); // <-- this ends the whole script normally (without throwing an error)
}
catch(err) {
// this crashes the script if the processes couldn't be shut down in the try{} block
console.log("\x1b[31m\x1b[1m Couldn't shut down all processes due to error: '" + err + "', crashing script instead.\x1b[0m");
process.exit(1); // <-- this ends the whole script and uses the exit code 1, which is an error
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment