doing a cleanup action just before node.js exits


I want to tell node.js to always do something just before it exits, for whatever reason - Ctrl+C, exception, or any other reason.

I tried this:

process.on('exit', function (){

Started the process, killed it, and nothing happened; started again, pressed Ctrl+C, and still nothing happened...

12/25/2012 3:07:26 PM

Accepted Answer


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}));
8/11/2018 1:32:13 PM

The script below allows having a single handler for all exit conditions. It uses an app specific callback function to perform custom cleanup code.


// 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;

  // do app specific cleaning before exiting
  process.on('exit', function () {

  // catch ctrl+c event and exit normally
  process.on('SIGINT', function () {

  //catch uncaught exceptions, trace, then exit normally
  process.on('uncaughtException', function(e) {
    console.log('Uncaught Exception...');

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

// Emits an uncaught exception when called because module does not exist
function error() {
  var x = require('');

// Try each of the following one at a time:

// Uncomment the next line to test exiting on an uncaught exception

// Uncomment the next line to test exiting normally
//setTimeout(function(){process.exit(3)}, 2000);

// Type Ctrl-C to test forced exit 

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow