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.
process.kill(pid, 'SIGTERM');
Try it
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.
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
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:pidis not a number.
Usage notes
- Success only means "delivered". It does not say the target stopped. A process that handles
SIGTERMcan ignore it; follow up withSIGKILLafter a timeout if you must be sure. SIGKILLcannot 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: trueand killing-child.pidstops the child and everything it started. Shell wrappers otherwise leave grandchildren running. - Windows: any signal except
0terminates the target without letting it handle anything.SIGINTandSIGTERMare 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.signalsmaps names to numbers on the current platform.