Open In App

How to kill all processes in NodeJS?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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.
  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.

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

Example 1:

index.js




// 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:

index.js




// 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:



Last Updated : 30 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads