446

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...

1

13 Answers 13

684

UPDATE:

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}));

This only works if you call synchronous code inside the handler, otherwise it will call the handler indefinitely

21
  • 6
    Is there a way to handle both Ctrl+C and a usual exit in the same place, or do I have to write two separate handlers? What about other types of exit, such as unhandled exception - there is a specific handler for that case, but should I handle this with a third copy of the same handler? Dec 26, 2012 at 9:04
  • 106
    Note that you must only perform synchronous operations in exit handler
    – Lewis
    Feb 10, 2015 at 14:23
  • 4
    @KesemDavid I think you should use the beforeExit event instead.
    – Lewis
    Nov 10, 2016 at 14:19
  • 42
    This solution has numerous issues. (1) It doesn't report signals to parent processes. (2) It doesn't convey the exit code to the parent process. (3) It doesn't allow for Emacs-like children that ignore Ctrl-C SIGINT. (4) It doesn't allow asynchronous cleanup. (5) It doesn't coordinate a single stderr message across multiple cleanup handlers. I've written a module that does all this, github.com/jtlapp/node-cleanup, originally based on the cleanup.js solution below, but greatly revised based on feedback. I hope it proves helpful.
    – Joe Lapp
    Dec 27, 2016 at 5:45
  • 3
    You must not call process.exit inside on("exit", ...) handler. This handler IS meant to be called after process.exit call nodejs.org/api/process.html#process_event_exit Consequences will be very bad, for example, all errors will be muted. Jan 12, 2018 at 17:47
205

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 
8
  • 18
    I've found this code indispensable and created a node package for it, with modifications, crediting you and this SO answer. Hope that's okay, @CanyonCasa. Thank you! npmjs.com/package/node-cleanup
    – Joe Lapp
    Sep 13, 2016 at 16:07
  • 4
    I love the cleanup. But I don't like the process.exit(0); cons.org/cracauer/sigint.html My feeling is you should let the kernel handle the destruction. You are not exiting the same way that a SIGINT. SIGINT doesn't exit with 2. You are mistaking the SIGINT with error code. They aren't the same. Actually Ctrl+C exist with 130. Not 2. tldp.org/LDP/abs/html/exitcodes.html
    – Banjocat
    Nov 18, 2016 at 2:44
  • 8
    I have rewritten npmjs.com/package/node-cleanup so that SIGINT handling works well with other processes, per @Banjocat's link. It also now correctly relays signals to the parent process instead of calling process.exit(). Cleanup handlers now have the flexibility to behave as a function of exit code or signal, and cleanup handlers can be uninstalled, as necessary to support asynchronous cleanup or prevent cyclic cleanup. It now has little resemblance to the above code.
    – Joe Lapp
    Dec 27, 2016 at 5:28
  • 4
    Forgot to mention that I also made a (hopefully) comprehensive test suite.
    – Joe Lapp
    Dec 27, 2016 at 5:36
  • 1
    Not sure if you can safely do process.emit('cleanup'); in the exit callback. nodejs docs state that "Listener functions must only perform synchronous operations. The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to be abandoned"
    – aromero
    May 28, 2019 at 22:33
83

This catches every exit event I can find that can be handled. Seems quite reliable and clean so far.

[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach((eventType) => {
  process.on(eventType, cleanUpServer.bind(null, eventType));
})
6
  • 3
    This is awesome! Oct 24, 2019 at 19:31
  • 1
    Great work, I am using it in production right now! Thanks a bunch!
    – randy
    Apr 26, 2020 at 19:26
  • Would be nice to get some info for these event or a link to resources
    – Sagiv b.g
    Jun 29, 2021 at 12:22
  • @Sagivb.g The process signals are documented in every *nix distro. Do man kill
    – oligofren
    Jan 31, 2022 at 18:56
  • 2
    Just remember - in your cleanup function - to call process.exit() in the end. Reason being, your function will override the default behavior, e.g. killing the port (see the Node documentation: nodejs.org/api/process.html#process_signal_events)
    – Liran H
    Jun 1, 2022 at 13:35
24

"exit" is an event that gets triggered when node finish it's event loop internally, it's not triggered when you terminate the process externally.

What you're looking for is executing something on a SIGINT.

The docs at http://nodejs.org/api/process.html#process_signal_events give an example:

Example of listening for SIGINT:

// Start reading from stdin so we don't exit.
process.stdin.resume();

process.on('SIGINT', function () {
  console.log('Got SIGINT.  Press Control-D to exit.');
});

Note: this seems to interrupt the sigint and you would need to call process.exit() when you finish with your code.

2
  • 2
    Is there a way to handle both Ctrl+C and a usual exit in the same place? Or do I have to write two identical handlers? Dec 25, 2012 at 20:49
  • Just as a note, if you have to end node by a kill command doing kill -2 will pass the SIGINT code. We have to do it this way because we have node logging to a txt file so Ctrl + C is not possible.
    – Aust
    Feb 14, 2013 at 23:22
13
function fnAsyncTest(callback) {
    require('fs').writeFile('async.txt', 'bye!', callback);
}

function fnSyncTest() {
    for (var i = 0; i < 10; i++) {}
}

function killProcess() {

    if (process.exitTimeoutId) {
        return;
    }

    process.exitTimeoutId = setTimeout(() => process.exit, 5000);
    console.log('process will exit in 5 seconds');

    fnAsyncTest(function() {
        console.log('async op. done', arguments);
    });

    if (!fnSyncTest()) {
        console.log('sync op. done');
    }
}

// https://nodejs.org/api/process.html#process_signal_events
process.on('SIGTERM', killProcess);
process.on('SIGINT', killProcess);

process.on('uncaughtException', function(e) {

    console.log('[uncaughtException] app will be terminated: ', e.stack);

    killProcess();
    /**
     * @https://nodejs.org/api/process.html#process_event_uncaughtexception
     *  
     * 'uncaughtException' should be used to perform synchronous cleanup before shutting down the process. 
     * It is not safe to resume normal operation after 'uncaughtException'. 
     * If you do use it, restart your application after every unhandled exception!
     * 
     * You have been warned.
     */
});

console.log('App is running...');
console.log('Try to press CTRL+C or SIGNAL the process with PID: ', process.pid);

process.stdin.resume();
// just for testing
2
  • 7
    This answer deserves all the glory, but because there is no explanation, there is no up-vote either unfortunately. What significant about this answer is, the doc says, "Listener functions must only perform synchronous operations. The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to be abandoned. ", and this answer overcomes that limitation!
    – xpt
    Feb 25, 2018 at 14:36
  • If I am reading this correctly, exiting will always take a fixed amount of time (five seconds), which may be too long or worse, too short, if the asynchronous operation needs more time.
    – Sebastian
    Dec 20, 2020 at 11:32
10

async-exit-hook seems to be the most up-to-date solution for handling this problem. It's a forked/re-written version of exit-hook that supports async code before exiting.

1
  • 1
    I ended up using this library as it allowed me to use timeouts, which is what I needed to stop gracefully Mar 1, 2021 at 17:16
9

Just wanted to mention death package here: https://github.com/jprichardson/node-death

Example:

var ON_DEATH = require('death')({uncaughtException: true}); //this is intentionally ugly

ON_DEATH(function(signal, err) {
  //clean up code here
})
1
  • Seems you have to explicitly exit the program with process.exit() within the callback. This tripped me up. Apr 27, 2020 at 6:37
5

I need to do an asynchronous cleanup action on exit, none of the answers in this question worked for me.

So I tried it myself, and finally found this:

process.once('uncaughtException', async () => {
  await cleanup()

  process.exit(0)
})

process.once('SIGINT', () => { throw new Error() })
1

After playing around with other answer, here is my solution for this task. Implementing this way helps me centralize cleanup in one place, preventing double handling the cleanup.

  1. I would like to route all other exiting codes to 'exit' code.
const others = [`SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`]
others.forEach((eventType) => {
    process.on(eventType, exitRouter.bind(null, { exit: true }));
})
  1. What the exitRouter does is calling process.exit()
function exitRouter(options, exitCode) {
   if (exitCode || exitCode === 0) console.log(`ExitCode ${exitCode}`);
   if (options.exit) process.exit();
}
  1. On 'exit', handle the clean up with a new function
function exitHandler(exitCode) {
  console.log(`ExitCode ${exitCode}`);
  console.log('Exiting finally...')
}

process.on('exit', exitHandler)

For the demo purpose, this is link to my gist. In the file, i add a setTimeout to fake the process running.

If you run node node-exit-demo.js and do nothing, then after 2 seconds, you see the log:

The service is finish after a while.
ExitCode 0
Exiting finally...

Else if before the service finish, you terminate by ctrl+C, you'll see:

^CExitCode SIGINT
ExitCode 0
Exiting finally...

What happened is the Node process exited initially with code SIGINT, then it routes to process.exit() and finally exited with exit code 0.

0

io.js has an exit and a beforeExit event, which do what you want.

0
0

To those of you running npm scripts that would like to perform an action before the process finishes through the script line definition without extra file definition you can just add a semicolon (checked on mac)

for example:

"start": "npm run add-hosts-to-hosts-file && npm run start ; npm run clear-hosts-from-hosts file",

this covers the scenario for Ctrl + C. Might also cover other cases.

-1

In the case where the process was spawned by another node process, like:

var child = spawn('gulp', ['watch'], {
    stdio: 'inherit',
});

And you try to kill it later, via:

child.kill();

This is how you handle the event [on the child]:

process.on('SIGTERM', function() {
    console.log('Goodbye!');
});
-4

Here's a nice hack for windows

process.on('exit', async () => {
    require('fs').writeFileSync('./tmp.js', 'crash', 'utf-8')
});
1
  • Not really a nice hack if you fail to supply some elaboration of what your code does and what goal it achieves. I can only guess that's why you got the downvotes.
    – oligofren
    Jan 31, 2022 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.