Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.kill() method

Reference for process.kill() in Node.js: sending SIGTERM or other signals to a process id, testing whether a process exists with signal 0, and common errors.

Despite its name, process.kill() does not necessarily kill anything. It sends a signal to the process with the given id. With the default SIGTERM signal that usually ends the target, but the target may catch the signal and decide for itself, and other signals mean other things.

You can use it on a child process you started, on any process your user is allowed to signal, or on your own process.pid. A special signal value 0 only checks whether the process exists.

NODE
process.kill(pid, 'SIGTERM');

Try it

Node.js Stopping a child process

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Probing and errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
process.kill(pid[, signal])
Name Description
pid Process id (a number). A negative value signals a whole process group on POSIX systems.
signal Signal name ('SIGINT') or number. Default 'SIGTERM'. 0 tests for existence.

Returns: true when the signal was delivered (or, for 0, the process exists).

Errors / edge cases:

  • ESRCH: no such process.
  • EPERM: you are not allowed to signal that process (it belongs to another user).
  • ERR_UNKNOWN_SIGNAL: the signal name is not known on this platform.
  • ERR_INVALID_ARG_TYPE: pid is not a number.

Usage notes

  • Success only means "delivered". It does not say the target stopped. A process that handles SIGTERM can ignore it; follow up with SIGKILL after a timeout if you must be sure.
  • SIGKILL cannot be caught, so the target gets no chance to clean up. Use it as a last resort.
  • For child processes, prefer child.kill(). It does the same for the child's pid, and it handles the case where the process has already ended.
  • Existence checks are racy. A pid may be reused by another process after yours ends. Do not use kill(pid, 0) for security decisions.
  • Process groups: on POSIX, spawning with detached: true and killing -child.pid stops the child and everything it started. Shell wrappers otherwise leave grandchildren running.
  • Windows: any signal except 0 terminates the target without letting it handle anything. SIGINT and SIGTERM are not delivered as events there.
  • Self-signalling is a handy way to test shutdown code, as in the signal events page.
  • Listing signals: os.constants.signals maps names to numbers on the current platform.