Fresheyeball
Fresheyeball

Reputation: 30015

Node.js process and exit codes not showing up

sig.js

process.on("exit", function(code){
  process.stdout.write("it was: " + code);
});

setTimeout(function(){
  console.log("times up");
}, 30000);

if I then run

nohup node sig.js &

and let the clock run out, I get the expected nohup.out file

it was: 0

but if I use kill -SIGINT <process id> then I get a nohup.out file that is completely empty. Based on the docs https://nodejs.org/api/process.html#process_event_exit I am expecting something... What am I missing here?

Upvotes: 1

Views: 100

Answers (1)

Brandon Smith
Brandon Smith

Reputation: 1197

From the linked docs:

This event is only emitted when Node.js exits explicitly by process.exit() or implicitly by the event loop draining.

The process is killed before the exit event is fired when you send a SIGINT.

You need to explicitly listen for the SIGINT event:

process.on("SIGINT", function () {
  //graceful shutdown
  process.exit();
});

This will catch the SIGINT and perform a graceful shutdown, which will force it through your expected code.

Upvotes: 1

Related Questions