Rohan Orton
Rohan Orton

Reputation: 1232

Emitting SIGINT event from inside node

I'm currently working with a library in node.js that happens to capture Ctrl-C and throw an error instead of allowing the normal behaviour (emitting a SIGINT event) to occur.

Wondering if it is possible to catch this error and emit a SIGINT event?

Upvotes: 4

Views: 4871

Answers (2)

Manuel Spigolon
Manuel Spigolon

Reputation: 12870

The best approach would be to use the process.kill function.

From docs:

The process.kill() method sends the signal to the process identified by pid.

Example:

process.on('SIGINT', () => {
  console.log('Got SIGINT signal.');
});

setTimeout(() => {
  console.log('Exiting.');
  process.exit(0);
}, 100);

process.kill(process.pid, 'SIGINT');

Upvotes: 5

gnerkus
gnerkus

Reputation: 12019

It is possible to do this within a Node process.

You can emit the 'SIGINT' event from the process object.

process.emit('SIGINT');

Upvotes: 6

Related Questions