Node JS - 无法终止使用子进程执行程序执行的进程

Node JS - Cannot Kill Process Executed with Child Process Exec

我们正在尝试终止使用节点 child_process exec 命令

启动的 chrome 浏览器的进程
var process = cp.exec(`"chrome.exe" --app="..."`, () => {}); // working great

但是当我们尝试

process.kill(); //nothing happens...

这个过程指的是chrome window还是别的?如果没有,我们如何获取新打开的chrome window进程,PID等...?

任何帮助都会很棒...

注意 - 我们已经尝试使用 chrome_launcher NPM 但它没有帮助,因为我们无法在没有全屏的情况下在信息亭模式下打开 chrome,但这是另一个问题的问题...

我无法添加评论,所以我直接在答案中说:

如果您勾选上面的link,您需要如下的库

https://www.npmjs.com/package/fkill

取自 Whosebug 问题的用法示例

const fkill = require('fkill');

fkill(1337).then(() => {
    console.log('Killed process');
});

fkill('Safari');

fkill([1337, 'Safari']);

我还找到了这个库来检查 运行 进程

https://github.com/neekey/ps

试试 PID hack

我们可以使用 {detached: true} 选项启动子进程,这样这些进程就不会附加到主进程,而是会转到一组新的进程。

然后在主进程上使用 process.kill(-pid) 方法,我们可以杀死所有在同一个 pid 组的子进程的同一组中的进程。就我而言,我在该组中只有一个进程。

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

var child = spawn('your-command', {detached: true});

process.kill(-child.pid);