Chapter 39: Node.js Process Management

Process Management in Node.js — written as if we are sitting together, with the terminal open, code editor ready, and I’m explaining everything slowly and clearly, with many realistic examples you can copy-paste and try right now.

We’ll cover:

  • What a “process” really is in Node.js
  • The most important properties and methods of the global process object
  • How to manage the current process (arguments, signals, exit, env, memory…)
  • How to create and manage child processes (the child_process module)
  • Real-world patterns used in production in 2025–2026
  • Common mistakes & best practices

Let’s go step by step.

1. The global process object – your window into the current Node.js process

process is a global object — you don’t need to import anything.

It gives you information about and control over the currently running Node.js process.

Most useful properties & methods (with real examples)

JavaScript

Very common pattern – read CLI flags nicely

JavaScript

2. Graceful shutdown & signal handling (very important in production)

Node.js processes can be told to stop in several ways:

  • Ctrl+C (SIGINT)
  • kill <pid> (default SIGTERM)
  • kill -9 <pid> (SIGKILL – cannot be caught)
  • Docker stop, PM2 restart, systemd, etc.

Best practice – graceful shutdown

JavaScript

What good production setups do

  • Listen for SIGTERM (Docker stop, Kubernetes, PM2, systemd)
  • Listen for SIGINT (Ctrl+C in dev)
  • Close HTTP server → stop accepting new requests
  • Wait a grace period (5–30 seconds)
  • Close DB connections, Redis, message queues, etc.
  • Exit with code 0 (success) or 1 (error)

3. Creating & managing child processes (child_process module)

Node.js is single-threaded for JavaScript execution. When you need to run CPU-heavy work or external commands, you spawn child processes.

JavaScript

worker.js (example)

JavaScript

When to use which

Method Use when… Output handling IPC (parent ↔ child messages) Typical use case
spawn Long-running process, streaming output Streams (stdout/stderr) No by default ffmpeg, image processing, git commands
exec Short command, you want full output at once Buffered (callback) No Quick shell commands, version checks
fork Run another Node.js script, need messaging Streams + IPC Yes Worker threads alternative, parallel tasks

4. Memory & CPU usage monitoring (very useful in production)

JavaScript

Very common in production Send this info to monitoring (Prometheus, Grafana, New Relic, Datadog…)

Summary – Quick cheat sheet for process management

You want to… Use this Typical real-world example
Read CLI arguments process.argv.slice(2) –port 4000 –debug
Read environment variables process.env.PORT Config, secrets, feature flags
Graceful shutdown process.on(‘SIGTERM’, …) + server.close() Docker stop, Kubernetes rolling update
Run external command (short) exec(‘git rev-parse HEAD’, callback) Get commit hash, run migrations
Run external process (long-running) spawn(‘ffmpeg’, args) Video processing, image optimization
Run another Node.js script with IPC fork(‘./worker.js’) Background tasks, parallel processing
Monitor memory usage process.memoryUsage() Health checks, alerts
Change process name in ps / top process.title = ‘my-api-server’ Easier debugging in production

Want to go much deeper into any specific area?

  • Full graceful shutdown example with DB, Redis, HTTP server
  • Building a real worker pool using fork + IPC
  • process.memoryUsage() monitoring + alerting patterns
  • Child process error handling & restart strategies
  • Using PM2, Docker, Kubernetes signals with Node.js

Just tell me which topic feels most useful right now — I’ll continue with concrete, production-ready examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *