How to Kill a Running Process on Windows from Nodejs

How to kill a running process on windows from nodejs

just use ps.kill

ps.kill('8092', function( err ) {
if (err) {
throw new Error( err );
}
else {
console.log( 'Process with pid 8092 has been killed!');
}
});

you can check the documentation for more info

How to kill an open process on node.js?

Use the following set of commands to identify the process running on a given port and to termiate it from the command line

   sudo fuser -v 5000/tcp // gives you the process running on port 5000

It will output details similar to the one shown below

                        USER        PID ACCESS COMMAND
5000/tcp: almypal 20834 F.... node

Then use

   sudo fuser -vk 5000/tcp

to terminate the process. Check once again using

   sudo fuser -v 5000/tcp

to ensure that the process has terminated.

On Windows you could use the following steps

  C:\> tasklist // will show the list of running process'

Image Name PID Session Name Session# Mem Usage
System 4 console 0 236 K
...
node.exe 3592 console 0 8440 k

Note the PID corresponding to your node process, in this case 3592. Next run taskkill to terminate the process.

  C:\> taskkill /F /PID 3592

Or /IM switch

  C:\> taskkill /F /IM node.exe

Close another process from node on Windows

According to the documentation, you simply access the property.

process.kill(process.pid, 'SIGKILL');

This is a theoretical, untested, psuedo function that may help explain what I have in mind

exec('tasklist', (err, out, code) => { //tasklist is windows, but run command to get proccesses
const id = processIdFromTaskList(processName, out); //get process id from name and parse from output of executed command
process.kill(id, "SIGKILL"); //rekt
});

stop all instances of node.js server

Windows Machine:

Need to kill a Node.js server, and you don't have any other Node processes running, you can tell your machine to kill all processes named node.exe. That would look like this:

taskkill /im node.exe

And if the processes still persist, you can force the processes to terminate by adding the /f flag:

taskkill /f /im node.exe

If you need more fine-grained control and need to only kill a server that is running on a specific port, you can use netstat to find the process ID, then send a kill signal to it. So in your case, where the port is 8080, you could run the following:

C:\>netstat -ano | find "LISTENING" | find "8080"

The fifth column of the output is the process ID:

  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       14828
TCP [::]:8080 [::]:0 LISTENING 14828

You could then kill the process with taskkill /pid 14828. If the process refuses to exit, then just add the /f (force) parameter to the command.



MacOS machine:

The process is almost identical. You could either kill all Node processes running on the machine:

killall node

Or also as alluded to in @jacob-groundwater's answer below using lsof, you can find the PID of a process listening on a port (pass the -i flag and the port to significantly speed this up):

$ lsof -Pi :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 1073 urname 22u IPv6 bunchanumbershere 0t0 TCP *:8080 (LISTEN)

The process ID in this case is the number underneath the PID column, which you could then pass to the kill command:

$ kill 1073

If the process refuses to exit, then just use the -9 flag, which is a SIGTERM and cannot be ignored:

$ kill -9 1073


Linux machine:

Again, the process is almost identical. You could either kill all Node processes running on the machine (use -$SIGNAL if SIGKILL is insufficient):

killall node

Or also using netstat, you can find the PID of a process listening on a port:

$ netstat -nlp | grep :8080
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1073/node

The process ID in this case is the number before the process name in the sixth column, which you could then pass to the kill command:

$ kill 1073

If the process refuses to exit, then just use the -9 flag, which is a SIGTERM and cannot be ignored:

$ kill -9 1073

How to terminate the process created by the child process created by childProcess.exec?

Ciao, you can try to follow this guide.

In brief, to kill a process in windows you could do:

    exec(`taskkill /PID ${pid} /T /F`, (error, stdout, stderr)=>{
console.log("taskkill stdout: " + stdout)
console.log("taskkill stderr: " + stderr)
if(error){
console.log("error: " + error.message)
}
})

start\kill local EXE application with Node.js

If you planning to kill your very own node process, the process API exposes the method exit(), which is indeed a wrapper to the C exit() method. According to the docs, it takes a parameter to specify success or failure.

One very "interesting" thing, then, would be to implement a controller to stop your server. Something like this:

app.post('/stop/server/now', function(){
process.exit(0);
});

EDIT To kill other processes, you simply need to know their pid (and have enough permission to kill other processes).

First, to get the pids, execute a command to do so. On Linux, this would be:

var exec = require('child_process').exec;

exec("ps aux | grep 'process_to_kill' | grep -v grep | awk '{print $2}'",
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
if (error !== null) {
console.log('exec output: ' + error);
}
});

Then, you pass these pids to the process.kill() API.

Kill a running node process in windows

  • Kill it by PID

    Stop-Process 3512
  • Kill it by process name

    Stop-Process -processname node*
  • Make someone else kill it, if it's launched as a child_process

    var exec = require('child_process').exec;

    var child = exec('node appium -p 3000');

    // Kill after 3 seconds
    setTimeout(function(){ child.kill(); }, 3000);
  • Kill itself

    process.exit();
  • If it's not a background process, kill it with CTRL+C.

You stated in your comment that it is indeed launched as a child process but you need to kill it in another function. This is a question of variable scope. The following will NOT work:

function launch(){
var child = spawn(cmd, args);
}

function kill(){
child.kill(); // child is undefined
}

You just need to make sure child exists in your scope:

var child;

function launch(){
child = spawn(cmd, args);
}

function kill(){
child.kill();
}


Related Topics



Leave a reply



Submit