【问题标题】:How to kill a running process on windows from nodejs如何从nodejs杀死Windows上正在运行的进程
【发布时间】:2018-10-28 12:14:53
【问题描述】:

我想通过nodejs关闭windows上的chrome应用。

我做了什么:

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.command, process.arguments );
//process.kill(8092)
}
else {
    console.log( 'No such process found!' );
}

});

我无法终止该进程。任何人都可以提出一种方法来做到这一点。 我尝试了 process.exit() process.kill process.abort,但没有任何效果。如果你能帮助我,那就太好了。

【问题讨论】:

    标签: javascript node.js kill-process


    【解决方案1】:

    您可以使用child_process.exec() 拨打taskkill。下面的 sn-p 使用taskkill 命令并杀死具有 PID 或 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)
      })
    })
    

    【讨论】:

    • 我不确定 kill 是否在窗口中可用。我们在 Windows 上使用 taskkill 对吗?
    • @TuanAnhTran 是我的错
    【解决方案2】:

    只需使用ps.kill

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

    您可以查看documentation 了解更多信息

    【讨论】:

    • 感谢您的帮助。还有一件事是,我可以使用名称而不是进程的 PID 进行此查找。如果是,我应该使用什么?
    • 您正在使用 PID 进行查找,因此您可能也需要它来终止进程。如果你没有它ps.lookup 给你一个进程,process.pid 包含它的 pid 用于ps.kill
    • 是的,我明白了你的意思,但我想知道我应该用什么来查找进程名称而不是 PID,然后用名称本身杀死它?
    • 你不能用名字杀人。您可能有更多具有相同名称的进程。这就是你需要pid的原因。因此,只需使用名称进行查找并使用 pid 终止。无论如何,查找都会为您提供一个数组,以防查询为您提供多个结果。
    【解决方案3】:

    试试这个,这是一个名为 tree-kill 的 npm 包。它会有效地杀死进程。

    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);
    });
    

    【讨论】:

      【解决方案4】:

      该调用有一个 npm 包taskkill

      const taskkill = require('taskkill');
      
      const input = [4970, 4512];
      
      taskkill(input).then(() => {
          console.log(`Successfully terminated ${input.join(', ')}`);
      });
      

      或者你可以看看那个包并复制相关代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 2019-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多