I want to tell Node.js to always do something just before it exits, for whatever reason — Ctrl+C, an exception, or any other reason.
I tried this:
process.on('exit', function (){ console.log('Goodbye!'); });
I started the process, killed it, and nothing happened. I started it again, pressed Ctrl+C, and still nothing happened...
When Node. js exits, it also emits several types of events. One of these is beforeExit , and as its name implies, it is emitted right before a Node process exits. You can provide an event handler which can make asynchronous calls, and the event loop will continue to perform the work until it's all finished.
Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.
exit() in your application causes the NodeJS instance to close. killall node in bash would kill all NodeJS instances running on your machine.
Exit code 1 is for when unhandled fatal exceptions occur that was not handled by the domain. process. exit(1); process. exit() is one of the methods for Node.
You can register a handler for process.on('exit')
and in any other case(SIGINT
or unhandled exception) to call process.exit()
process.stdin.resume();//so the program will not close instantly function exitHandler(options, exitCode) { if (options.cleanup) console.log('clean'); if (exitCode || exitCode === 0) console.log(exitCode); 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}));
The script below allows having a single handler for all exit conditions. It uses an app specific callback function to perform custom cleanup code.
cleanup.js
// Object to capture process exits and call app specific cleanup function function noOp() {}; exports.Cleanup = function Cleanup(callback) { // attach user callback to the process event emitter // if no callback, it will still exit gracefully on Ctrl-C callback = callback || noOp; process.on('cleanup',callback); // do app specific cleaning before exiting process.on('exit', function () { process.emit('cleanup'); }); // catch ctrl+c event and exit normally process.on('SIGINT', function () { console.log('Ctrl-C...'); process.exit(2); }); //catch uncaught exceptions, trace, then exit normally process.on('uncaughtException', function(e) { console.log('Uncaught Exception...'); console.log(e.stack); process.exit(99); }); };
This code intercepts uncaught exceptions, Ctrl+C and normal exit events. It then calls a single optional user cleanup callback function before exiting, handling all exit conditions with a single object.
The module simply extends the process object instead of defining another event emitter. Without an app specific callback the cleanup defaults to a no op function. This was sufficient for my use where child processes were left running when exiting by Ctrl+C.
You can easily add other exit events such as SIGHUP as desired. Note: per NodeJS manual, SIGKILL cannot have a listener. The test code below demonstrates various ways of using cleanup.js
// test cleanup.js on version 0.10.21 // loads module and registers app specific cleanup callback... var cleanup = require('./cleanup').Cleanup(myCleanup); //var cleanup = require('./cleanup').Cleanup(); // will call noOp // defines app specific callback... function myCleanup() { console.log('App specific cleanup code...'); }; // All of the following code is only needed for test demo // Prevents the program from closing instantly process.stdin.resume(); // Emits an uncaught exception when called because module does not exist function error() { console.log('error'); var x = require(''); }; // Try each of the following one at a time: // Uncomment the next line to test exiting on an uncaught exception //setTimeout(error,2000); // Uncomment the next line to test exiting normally //setTimeout(function(){process.exit(3)}, 2000); // Type Ctrl-C to test forced exit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With