← Back to Node.js, page 4

nodejs-0077 · Easy fresher

What are process signals (SIGINT, SIGTERM, SIGKILL, SIGHUP, SIGUSR1) and how do you handle them in Node.js?

A signal is a small message that the operating system sends to a process. SIGINT is Ctrl+C, SIGTERM is a polite request to stop, SIGHUP means the terminal closed, SIGUSR1 and SIGUSR2 are free for your own use (Node uses SIGUSR1 to start the inspector). SIGKILL and SIGSTOP cannot be caught. Handle signals with process.on('SIGTERM', handler).

Full answer

Signals are how the operating system or another process asks a program to do something.

Common signals:

  • SIGINT (2): interrupt from the keyboard, Ctrl+C. Node exits by default.
  • SIGTERM (15): terminate. The normal polite stop, sent by kill <pid>, Docker stop, Kubernetes and process managers. Node exits by default unless you handle it.
  • SIGKILL (9): immediate stop by the kernel. The process cannot handle, delay or even see it. Orchestrators send it after the grace period.
  • SIGHUP (1): terminal closed. Daemons often use it to reload configuration.
  • SIGUSR1: Node reserves it to start the debugger (inspector). SIGUSR2: free for applications, for example to dump a heap snapshot (--heapsnapshot-signal=SIGUSR2).
  • SIGPIPE: writing to a closed pipe. Node ignores it and gives you an EPIPE error instead.

In Node:

  • process.on('SIGTERM', fn) registers a handler. Once any handler exists, the default exit behaviour is removed, so your handler must end the process (or start the shutdown) itself.
  • process.once avoids running a handler twice.
  • process.kill(pid, 'SIGTERM') sends a signal to any process you own. child.kill('SIGTERM') does it for a child.
  • On Windows only SIGINT, SIGTERM, SIGHUP and SIGBREAK are partly emulated, and kill() terminates the process unconditionally.

Because SIGKILL cannot be handled, never rely on cleanup code for correctness. Design so that a hard kill is safe: idempotent jobs, transactions and short lock leases.

javascript
const { spawn } = require('node:child_process');

const childCode = `
process.on('SIGTERM', () => {
  console.log('child: got SIGTERM, cleaning up');
  process.exit(0);
});
process.on('SIGUSR2', () => console.log('child: got SIGUSR2, dumping a report'));
process.send('ready');
setInterval(() => {}, 1000); // keep the child alive
`;

function runChild(signalSequence) {
  return new Promise((resolve) => {
    const child = spawn(process.execPath, ['-e', childCode], { stdio: ['ignore', 'pipe', 'inherit', 'ipc'] });
    let output = '';
    child.stdout.on('data', (chunk) => (output += chunk));
    child.once('message', async () => {
      for (const signal of signalSequence) {
        child.kill(signal);
        await new Promise((r) => setTimeout(r, 100));
      }
    });
    child.on('exit', (code, signal) => resolve({ code, signal, output: output.trim() }));
  });
}

async function main() {
  const polite = await runChild(['SIGUSR2', 'SIGTERM']);
  console.log('SIGUSR2 then SIGTERM ->', polite);

  const brutal = await runChild(['SIGKILL']);
  console.log('SIGKILL ->', brutal);
}

main();

The child handles SIGTERM and SIGUSR2. With SIGKILL the child gets no chance to print anything, and the parent sees the signal name instead of an exit code. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): SIGUSR2 then SIGTERM -> { code: 0, signal: null, output: 'child: got SIGUSR2, dumping a report\nchild: got SIGTERM, cleaning up' } SIGKILL -> { code: null, signal: 'SIGKILL', output: '' }

Key points

  • SIGTERM asks politely, SIGKILL cannot be caught.
  • Registering a handler removes the default exit.
  • SIGUSR1 starts the inspector in Node.
  • Design for a hard kill as well.

Common mistakes

  • Adding a SIGTERM handler that never exits the process.
  • Expecting to catch SIGKILL.
  • Assuming signal behaviour on Windows is the same as on Linux.
  • Running the app through a shell script that does not forward signals.

Follow-ups

  • Why does a container sometimes ignore SIGTERM?
  • How would you reload config on SIGHUP?
  • What is the difference between kill and kill -9?

Tips

  • Give one line for each of the main signals.
  • Say 'cannot be caught' for SIGKILL.
  • Mention that handlers replace the default.