admin管理员组

文章数量:1287882

I want to close chrome application on windows through nodejs.

Here what I did :

var ps = require('ps-node');
ps.lookup({ pid: 8092 }, function(err, resultList ) {
if (err) {
    throw new Error( err );
}

var process = resultList[ 0 ];

if( process ){

    console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, processmand, process.arguments );
//process.kill(8092)
}
else {
    console.log( 'No such process found!' );
}

});

I am not able to kill the process. Can anyone suggest a way to do it. I tried process.exit() process.kill process.abort, but nothing worked for me. It would be great if you help me out.

I want to close chrome application on windows through nodejs.

Here what I did :

var ps = require('ps-node');
ps.lookup({ pid: 8092 }, function(err, resultList ) {
if (err) {
    throw new Error( err );
}

var process = resultList[ 0 ];

if( process ){

    console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.mand, process.arguments );
//process.kill(8092)
}
else {
    console.log( 'No such process found!' );
}

});

I am not able to kill the process. Can anyone suggest a way to do it. I tried process.exit() process.kill process.abort, but nothing worked for me. It would be great if you help me out.

Share Improve this question edited May 18, 2018 at 6:14 ra_pri asked May 18, 2018 at 6:09 ra_prira_pri 1772 gold badges5 silver badges20 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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

You can use child_process.exec() to call taskkill. The below snippet uses the taskkill mand and killing a process with either a PID or filename of an exe.

const {exec} = require('child_process')

const pid = 8092

// example app name 
const appName = 'firefox.exe' 

// Kills a PID and all child process
exec(`taskkill /pid ${pid} /t`, (err, stdout, stderr) => {
    if (err) {
      throw err
    }

    console.log('stdout', stdout)
    console.log('stderr', err)
  })
})

// Kills a process based on filename of the exe and all child processes
exec(`taskkill /im ${appName} /t`, (err, stdout, stderr) => {
    if (err) {
      throw err
    }

    console.log('stdout', stdout)
    console.log('stderr', err)
  })
})

There's a npm package for that call taskkill

const taskkill = require('taskkill');

const input = [4970, 4512];

taskkill(input).then(() => {
    console.log(`Successfully terminated ${input.join(', ')}`);
});

Or you can take a look at that package and copy the relevant code.

Try this, this is an npm package called tree-kill. And it will kill processes effectively.

var kill  = require('tree-kill');
const spawn = require('child_process').spawn;

var scriptArgs = ['myScript.sh', 'arg1', 'arg2', 'youGetThePoint'];
var child = spawn('sh', scriptArgs);

// some code to identify when you want to kill the process. Could be
// a button on the client-side??
button.on('someEvent', function(){
    // where the killing happens
    kill(child.pid);
});

本文标签: javascriptHow to kill a running process on windows from nodejsStack Overflow