Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching SIGTERM vs catching SIGINT

Tags:

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?

like image 843
Alexander Mills Avatar asked Feb 25 '17 00:02

Alexander Mills


People also ask

Should I use SIGINT or SIGTERM?

SIGTERM is the preferred way as the process has the chance to terminate gracefully. As a process can override the default action for SIGINT, SIGTERM, and SIGQUIT, it can be the case that neither of them finishes the process.

Can SIGTERM be caught?

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.

What is the difference between SIGTERM and SIGKILL?

SIGTERM vs SIGKILL:SIGTERM gracefully kills the process whereas SIGKILL kills the process immediately. SIGTERM signal can be handled, ignored and blocked but SIGKILL cannot be handled or blocked. SIGTERM doesn't kill the child processes. SIGKILL kills the child processes as well.

What happens when a process receives SIGTERM?

If a process receives SIGTERM, some other process sent that signal. SIGTERM is the signal that is typically used to administratively terminate a process. That's not a signal that the kernel would send, but that's the signal a process would typically send to terminate (gracefully) another process.


2 Answers

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.

like image 192
xpt Avatar answered Oct 01 '22 18:10

xpt


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

like image 24
Jim Factor Avatar answered Oct 01 '22 19:10

Jim Factor