How to kill all processes in NodeJS?
Last Updated :
26 Jun, 2024
To kill all Node.js processes In Node.js you can use process.kill method
, you can list all running processes, filter out the Node.js processes, and then use process.kill
to terminate each identified process.
process.kill
The process.kill( pid[,signal] ) is an inbuilt technique in node.js that conveys a message to the cycle, pid (which is the interaction id) and sign are in the string design that is the sign to send.
Syntax:
process.kill(pid[, signal])
Boundaries:
This technique acknowledges two boundaries as referenced above and depicted underneath:
- pid: This boundary holds the interaction ID.
- signal: This boundary holds the string design.
- signal names: These are in string design.
- SIGTERM
- SIGINT
- SIGHUP
Note: If no sign is indicated, then, at that point, as a matter of course 'SIGTERM' will be the sign.
- 'SIGTERM' and 'SIGINT' signals have default controllers on non-Windows stages that reset the terminal mode prior to leaving with code 128 + signal number. Assuming one of these signs has an audience introduced, its default conduct on node.js will be taken out.
- 'SIGHUP' is produced when the control center window is shut.
Return esteem:
The process.kill() strategy will toss a blunder on the off chance that the objective pid isn't found or doesn't exist. This technique returns boolean worth 0 assuming that pid exists and can be utilized as a test for the presence of the objective interaction. For window clients, this technique will likewise toss a mistake assuming pid is utilized to kill a gathering of interaction.
Beneath models show the utilization of the process.kill() property to kill processes in Node.js.
Example 1: This Node.js program demonstrates the process.kill
method to handle and terminate a process. It sets up a handler for the SIGINT
signal, prints a message when the signal is received, and schedules the process to exit after a short delay. The process is then terminated using its own PID with the SIGINT
signal.
Node
// index.js
// Printing process signal recognized
const displayInfo = () => {
console.log('Receiving SIGINT signal in nodeJS.');
}
// Starting a cycle
process.on('SIGINT', displayInfo);
setTimeout(() => {
console.log('Exiting.');
process.exit(0);
}, 100);
// kill the cycle with pid and sign = 'SIGINT'
process.kill(process.pid, 'SIGINT');
Run the application:
node index.js
Output:

Example 2: The Node.js script above sets up an event listener for the SIGHUP
signal, logs a message when the signal is received, and schedules the process to exit after a brief delay. Finally, it terminates the process using process.kill
with its own PID and the SIGHUP
signal.
Node
// index.js
// Printing process signal recognized
const displayInfo = () => {
console.log('Acknowledged SIGHUP signal in nodeJS.');
}
// Starting an interaction
process.on('SIGHUP', displayInfo);
setTimeout(() => {
console.log('Exiting.');
process.exit(0);
}, 100);
// kill the cycle with pid and sign = 'SIGHUP'
process.kill(process.pid, 'SIGHUP');
Run the application:
node index.js
Output:
