Catching SIGTERM vs catching SIGINT
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.
- 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.
- The description around command
killis 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
- 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
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.
SIGINT is generated by the user pressing Ctrl+C and is an
SIGTERM is a signal that is sent to request the process terminates. The kill command sends a
SIGTERM and it's a
You can catch both
SIGINT and you will always be able to close the process with a
kill -9 [pid].