Catching SIGTERM vs catching SIGINT
Asked Answered
T

2

41

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?

Therewith answered 25/2, 2017 at 0:31 Comment(0)
C
35

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].

Cascabel answered 25/2, 2017 at 0:39 Comment(0)
H
42

The accepted answer was mainly focused on OP's question of

In Node.js servers, is there any difference between catching SIGTERM vs catching SIGINT? Am I able to trap both signals and prevent exit?

I landed here because I want to know the differences between them. So here is a bit of more clarifications.

  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 a bit unclear.

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

The more clearer way to put it is, you are not allowed to trap the SIGKILL signal, but you can trap SIGINT and SIGTERM; even if both are caught in the program and get ignored, SIGKILL - kill -9 pid can still kill it.

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, to summary from the above wiki:

  • The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Ctrl+C.
  • The SIGTERM signal is sent to a process to request its termination. Unlike the SIGKILL signal, it can be caught and interpreted or ignored by the process. This allows the process to perform nice termination releasing resources and saving state if appropriate. SIGINT is nearly identical to SIGTERM.

So regarding the title of "Catching SIGTERM vs catching SIGINT (in Node.js)", if you want to quit gracefully, like handling Ctrl+C, trapping SIGINT alone is good enough, no need to handle both. Outside the scope of Node.js, it is a different story, check Nicholas Pipitone's comment.

Handbook answered 24/2, 2018 at 5:3 Comment(12)
I think there's some confusion over the wording. "You can catch both of them (SIGINT and SIGTERM) and still be able to close the process with a SIGKILL - kill -9 pid". That statement is correct. The accepted answer is right.Katelyn
My point was catching either of them (SIGINT and SIGTERM) has nothing to do with SIGKILL. I.e. do you need to catche either of them (SIGINT and SIGTERM) for closing the process with a SIGKILL - kill -9 pid to work? @KatelynHandbook
Uh, yea, they're three different signals. You can catch SIGINT and SIGTERM, and do whatever, and still be killed with a SIGKILL. You don't have to do that, but the OP was asking for clarification on the differences between the signals. The given scenario is just that. You can ignore SIGINT and SIGTERM, but you can't ignore SIGKILL. No one's really wrong here, lol.Katelyn
Agree, but I think OP has a better summary, "you are not allowed to trap the SIGKILL signal, but you can trap SIGINT and SIGTERM?" which should be acknowledged, instead of being blurred.Handbook
The reason why they have to be different is because SIGTERM specifically occurs when the program should be terminated. SIGINT just means the user pressed ctrl+c, which sometimes means terminated, but not always. In VIM for example, ctrl+c could be an actual command that is used in the editor, so SIGINT is only used to tell VIM "oh, the user meant ctrl+c". Or, consider bash itself. When you ctrl+c, ie giving bash a SIGINT, bash's job is to take whatever process is running, and give that program the SIGINT (whatever program that might be). if you give bash SIGTERM, it'll die.Zaratite
I think you did a good of clarifying a correct but awkwardly stated answer.Calibrate
Well thanks for the down vote @CraigHicks first of all. And second of all, please note that the "correct" answer is still the wrong one, and all my point was explaining why it is wrong, then establish what is correct, which is only the second important thing in such situation. I suggest that you not just standing there criticizing others, but give your fabulous answer instead, and let people be the judge how fabulous yours is over mine.Handbook
@Handbook - "SIGINT is generated by the user pressing Ctrl+C and is an interrupt". Therefore it is not the same as SIGTERM. Your answer contributes to the chosen answer, but does not override it. I compliment you again for your good work clarifying the selected answer. Only your claim that it is wrong, is wrong. Have a nice day.Calibrate
Ok I stand corrected @CraigHicks, and changed my wording, since it is now at the top of the answer. I.e., it has more responsibilities than trying to clarify on the accepted answer alone.Handbook
Would it be correct behaviour to process.exit() when catching SIGTERM? Just doing server.close() will not end the nodejs process.Angulo
@Marc, SIGTERM means the program should be terminated, so yes, process.exit() is a must, but graceful termination should be done before that, like server.close() etc, i.e., basically agree with you, but not just bluntly calling process.exit() without any graceful termination.Handbook
Note that Windows may send SIGBREAK so it's worth listening for that, too.Caniff
C
35

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].

Cascabel answered 25/2, 2017 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.