DEV Community

ujwala p
ujwala p

Posted on

Understanding SIGINT vs SIGTERM in Node.js (A Beginner-Friendly Guide)

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
Enter fullscreen mode Exit fullscreen mode

Suppose you're running:

node app.js
Enter fullscreen mode Exit fullscreen mode

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);
});
Enter fullscreen mode Exit fullscreen mode

Output:

SIGINT received
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

or

kill -15 <pid>
Enter fullscreen mode Exit fullscreen mode

Both send a SIGTERM signal.

Node.js can handle it like this:

process.on("SIGTERM", () => {
  console.log("SIGTERM received");
  process.exit(0);
});
Enter fullscreen mode Exit fullscreen mode

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:

  1. Stop accepting new requests.
  2. Finish ongoing work.
  3. Close database connections.
  4. Close RabbitMQ and Redis connections.
  5. Flush logs.
  6. Exit safely.

Docker Uses SIGTERM

When you run:

docker stop <container>
Enter fullscreen mode Exit fullscreen mode

Docker first sends:

SIGTERM
Enter fullscreen mode Exit fullscreen mode

Your application gets a few seconds to shut down gracefully.

If it doesn't exit in time, Docker sends:

SIGKILL
Enter fullscreen mode Exit fullscreen mode

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"));
Enter fullscreen mode Exit fullscreen mode

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)