我正在尝试编写一个简单的 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 true 和 detached true nodejs 来终止生成进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44007825/