Open In App

How to kill all processes in NodeJS?

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:



  1. SIGTERM
  2. SIGINT
  3. SIGHUP

Note: If no sign is indicated, then, at that point, as a matter of course ‘SIGTERM’ will be the sign.

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 in Node.js.

Example 1:




// Node.js program to show the
// process.kill(pid[, signal]) strategy
  
// 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:




// Node.js program to exhibit the
// process.kill(pid[, signal]) technique
// 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:


Article Tags :