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?
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.
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.
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.
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.
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.
SIGTERM The SIGTERM signal is sent to a process to request its termination... SIGINT is nearly identical to SIGTERM.
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.
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]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With