node.jssigintsigterm

Catching SIGTERM vs catching SIGINT


In Node.js servers, is there any difference between catching SIGTERM vs catching SIGINT?

I thought processes were not supposed to be able to prevent shutdown upon a SIGINT?

  process.once('SIGINT', function (code) {
    console.log('SIGINT received...');
    server.close();
  });

 // vs.

  process.once('SIGTERM', function (code) {
    console.log('SIGTERM received...');
    server.close();
  });

Am I able to trap both signals and prevent exit? My experimentation suggests the answer is yes, but from what I have read, SIGINT is always suppose to shutdown a process.

Or maybe I am confusing SIGINT with SIGKILL? Maybe SIGKILL is the signal that I cannot recover from?

Trapping these signals of course allows me to gracefully shutdown:

server.once('close', function(){
    // do some other stuff
    process.exit(2); // or whatever code pertains
});

I think I am confusing SIGINT with SIGKILL -

if I try to do this:

 process.once('SIGKILL', function (code) {
    console.log('SIGKILL received...');
    exitCode = code || 2;
    server.close();
  });

I get this error:

 internal/process.js:206
        throw errnoException(err, 'uv_signal_start');
        ^
    Error: uv_signal_start EINVAL
        at exports._errnoException (util.js:1022:11)
        at process.<anonymous> (internal/process.js:206:15)
        at emitTwo (events.js:106:13)
        at process.emit (events.js:191:7)
        at _addListener (events.js:226:14)
        at process.addListener (events.js:275:10)
        at process.once (events.js:301:8)

So apparently you are not allowed to trap the SIGKILL signal, but you can trap SIGINT and SIGTERM?


Solution

  • From https://en.wikipedia.org/wiki/Unix_signal:

    SIGINT is generated by the user pressing Ctrl+C and is an interrupt

    SIGTERM is a signal that is sent to request the process terminates. The kill command sends a SIGTERM and it's a terminate

    You can catch both SIGTERM and SIGINT and you will always be able to close the process with a SIGKILL or kill -9 [pid].