Understanding SIGINT vs SIGTERM in Node.js (A Beginner-Friendly Guide)
When I first started working with Node.js, I thought SIGINT and SIGTERM were just two different ways to stop a process.
After learning how production applications actually shut down, I realized they have very different purposes.
Let's understand them with simple examples.
What is a Signal?
A signal is a message sent by the operating system to a running process.
It tells the process that something has happened or asks it to perform an action.
Examples:
- Stop the process
- Pause the process
- Resume the process
- Reload configuration
Two of the most common signals are SIGINT and SIGTERM.
What is SIGINT?
SIGINT (Signal Interrupt) is sent when a user interrupts a running application.
The most common example is pressing:
Ctrl + C
Suppose you're running:
node app.js
When you press Ctrl + C, the terminal sends a SIGINT signal to your Node.js application.
You can listen for it like this:
process.on("SIGINT", () => {
console.log("SIGINT received");
process.exit(0);
});
Output:
SIGINT received
This is mainly used during local development.
What is SIGTERM?
SIGTERM (Signal Terminate) is a polite request asking a process to shut down gracefully.
Unlike SIGINT, it usually comes from another process or the operating system.
For example:
kill <pid>
or
kill -15 <pid>
Both send a SIGTERM signal.
Node.js can handle it like this:
process.on("SIGTERM", () => {
console.log("SIGTERM received");
process.exit(0);
});
Why Graceful Shutdown Matters
Imagine your application is:
- Processing payments
- Consuming RabbitMQ messages
- Writing data to PostgreSQL
- Uploading files
- Serving HTTP requests
If the application stops immediately, you could lose data or leave operations incomplete.
Instead, a graceful shutdown lets the application:
- Stop accepting new requests.
- Finish ongoing work.
- Close database connections.
- Close RabbitMQ and Redis connections.
- Flush logs.
- Exit safely.
Docker Uses SIGTERM
When you run:
docker stop <container>
Docker first sends:
SIGTERM
Your application gets a few seconds to shut down gracefully.
If it doesn't exit in time, Docker sends:
SIGKILL
which immediately terminates the process.
Kubernetes Uses SIGTERM Too
When a Pod is deleted, Kubernetes sends SIGTERM to the application.
The application has a grace period (30 seconds by default) to clean up before Kubernetes forcefully terminates it with SIGKILL.
Handling Both Signals
A production-ready Node.js application usually listens for both signals.
function shutdown(signal) {
console.log(`${signal} received`);
// Close HTTP server
// Close database connection
// Close RabbitMQ
// Close Redis
// Flush logs
process.exit(0);
}
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
Quick Comparison
| SIGINT | SIGTERM |
|---|---|
| Triggered by Ctrl + C | Triggered by kill, Docker, Kubernetes |
| Mainly used during development | Mainly used in production |
| Interrupts the process | Requests a graceful shutdown |
| Can be handled | Can be handled |
Final Thoughts
Understanding process signals is a small topic that has a huge impact in production systems.
Knowing the difference between SIGINT, SIGTERM, and SIGKILL helps you write applications that shut down cleanly, prevent data loss, and behave correctly inside Docker, Kubernetes, and other production environments.
Every backend developer working with Node.js should be familiar with these concepts.
Happy learning! 🚀
Top comments (0)