DEV Community

Mindy Zwanziger
Mindy Zwanziger

Posted on

Ohhh, a SIGTERM signal!

Red "slo" sign among green trees on modern city street, Photo by Tobi Law from Pexels

Fun fact: When you use Ctrl + C to stop your application, that sends a SIGTERM signal.

Oh a SIGTERM signal! 🐧🧊
What's a SIGTERM? 🐧
What's a signal? 🧊

A signal is basically a road sign for a computer. Stop, slow down, prepare for a stop, road closed, etc... It's referred to as a form of "inter-process communication" or IPC and is typically used in POSIX-compliant operating systems. POSIX being "Portable Operating System Interface" which is just a set of standards used to help operating systems work well-ish with each other.

A SIGTERM is a type of signal, namely of the "prepare for a stop" variety. It's a type of signal that your code can "catch" and use - usually to wrap up any processes you have going on before the application shuts down fully. This is in contrast to SIGKILL, which just shuts down automatically.

Graceful Shutdowns

Why is this important? You can "catch" and handle a received SIGTERM to perform a graceful shutdown of an application.

A graceful shutdown can include:

  • Stopping new requests from coming in
  • Finishing any ongoing requests
  • Cleaning up resources (like database connections)

It leaves your app, resources, and users in a happy place.

For some apps, this will require a significant amount of configuration. Sometimes though, all you need is a timeout to let a process finish:

process.on('SIGTERM', shutdown);

const shutdown = () => {
  const gracePeriodInMS = 30000;

  setTimeout(() => {
  }, gracePeriodInMs);

Code well, my friends!

More resources:
Graceful Shutdown in NodeJS
Linux Manual Page - Signals
Graceful Shutdown with NodeJS and Kubernetes: I particularly like the "How does it work" image here. I'm all about the visual representations!

Top comments (0)