Skip to content Skip to sidebar Skip to footer

Sigint Handler In Nodejs App Not Called For Ctrl-c (mac)

My code: process.on('SIGINT', function() { console.log('Interrupted'); process.exit(); }); // A big loop here that takes several seconds to execute through. console.log('Exit

Solution 1:

// Begin reading from stdin so the process does notexit imidiately
process.stdin.resume();
process.on('SIGINT', function() {
  console.log('Interrupted');
  process.exit();
});

https://nodejs.org/api/process.html#process_signal_events

EDIT:

// A big loop here that takes several seconds to execute through.

This is your problem. NodeJS is single-tread. When you perform sync loop you blocks event-loop and other event listeners wont be able work at the same time.

You can:

  1. Make loop async (recursive function with process.nextTick())
  2. Spawn child-process (worker) for loop job, then main event-loop will wont be blocked

Post a Comment for "Sigint Handler In Nodejs App Not Called For Ctrl-c (mac)"