javascript - 尝试使用 shell tr​​ue 和 detached true nodejs 来终止生成进程

标签 javascript node.js child-process spawn

我正在尝试编写一个简单的 node.js 应用程序,以便在专用 shell 中运行 selenium,然后关闭 shell 和相应的子进程(目前在 5 秒超时后进行测试)。 这是我到目前为止的代码:

const spawn = require('child_process').spawn;
const seleniumHub = spawn('java', ['-jar', 'selenium-server-standalone-2.48.2.jar'], { shell: true , detached: true });

seleniumHub.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
});

seleniumHub.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`);
});

seleniumHub.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
});

setTimeout(function () {
    // process.kill(seleniumHub.pid); no errors but shell not closed
    // seleniumHub.kill('SIGINT'); completely ignored 
}, 5000);

Selenium 正确运行,但我无法关闭打开的 shell。 如果我尝试通过 pid 关闭它,则会收到 pid 不存在的错误。 如果我尝试在子进程上调用 kill 方法,它会完全忽略该命令。请问有什么建议吗?

谢谢

附:我在 Windows 机器上

最佳答案

好的,我自己找到了解决方案,如果它对某人有用,我会将其发布在这里。 作为解决方法,我再次使用 spawn 并调用 Windows 的 taskkill,传递 f 和 t 作为参数:

spawn("taskkill", ["/pid", seleniumHub.pid, '/f', '/t']);

关于javascript - 尝试使用 shell tr​​ue 和 detached true nodejs 来终止生成进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44007825/

相关文章:

javascript 在 ie8 中不工作

javascript - Html5-使用 SVG 路径绘制的组织层次结构在左侧被剪裁

javascript - 当同时注入(inject)数据时,异步数据在 Material 底板组件中不可用

node.js - 如何在 graphql 解析器中同时返回错误和数据?

javascript - 如何在超时后杀死 spawnSync

javascript - 接受不在列表中的值 - bootstrap 组合框

node.js - Node JS-表达 : Unable to retrieve value from HTTP Context for POST and PUT requests while it works for GET

node.js - html 文件中的动态内容, Node

node.js child_process 生成忽略等号

node.js - 在node中使用ffmpeg,而不使用fluid-ffmpeg