Catching SIGTERM vs catching SIGINT

Alexander Mills picture Alexander Mills · Feb 25, 2017 · Viewed 21.7k times · Source

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?

Answer

xpt picture xpt · Feb 24, 2018

The accepted answer is wrong.

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

SIGTERM The SIGTERM signal is sent to a process to request its termination... SIGINT is nearly identical to SIGTERM.

  1. The description around command kill is incorrect.

You can catch both of them and still be able to close the process with a SIGKILL - kill -9 pid

That's wrong. Again, from above wiki:

The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal.

So, all in all,

SIGINT is nearly identical to SIGTERM.