[英]Child process not killed - Node js ( inside Electron js app, windows)
I am trying to stop this command after 4 seconds, but it doesn't stop the child-process (Running in an electron-js app, windows )我试图在 4 秒后停止此命令,但它不会停止子进程(在 electron-js 应用程序中运行,windows )
child = child_process.spawn("ping google.com -t", {
encoding: 'utf8',
shell: true
});
child.stdout.on('data', (data) => {
//Here is the output
data=data.toString();
console.log(data)
});
setTimeout(function() {
console.log('Killing Your child', child.pid);
child.kill()
}, 4 * 1000);
the timeout runs after 4 seconds, it logs the message but never stops the process超时 4 秒后运行,它记录消息但从不停止进程
I tried using tags inside the kill() like "SIGINT" or "SIGKILL"我尝试在 kill() 中使用标签,例如“SIGINT”或“SIGKILL”
I tried running without the timeout too...我也尝试在没有超时的情况下运行...
By looking at the official documentation for child process I have slightly modified your provided example to a bare minimum of what still seems to work for me.通过查看子进程的官方文档,我稍微修改了您提供的示例,使其看起来仍然对我有用。 Please note that the
ping -t
flag requires an argument which you didn't provide.请注意,
ping -t
标志需要您未提供的参数。
The following worked for me (the program exits after 4 pings) on Node LTS (16.15.1) and the latest version (18.4.0):以下在 Node LTS (16.15.1) 和最新版本 (18.4.0) 上对我有用(程序在 4 次 ping 后退出):
const { spawn } = require('child_process');
const child = spawn("ping", ['www.google.com'], {});
child.stdout.on('data', (data) => { console.log(data.toString()); });
setTimeout(() => {
console.log('Killing Your child', child.pid);
child.kill();
}, 4000);
shell: true
doesn't work with .kill()
as you can see in the answer from this post: shell: true
不适用于.kill()
,正如您在这篇文章的答案中看到的那样:
Try to kill a spawn process with shell true and detached true nodejs 尝试使用 shell true 和分离的 true nodejs 终止生成进程
I needed to replace:我需要更换:
child.kill()
with和
child_process.spawn("taskkill", ["/pid", child.pid, '/f', '/t']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.