为什么我不能在 Windows 上的 nodejs 中杀死我的子进程? [英] Why can I not kill my child process in nodejs on windows?

查看:55
本文介绍了为什么我不能在 Windows 上的 nodejs 中杀死我的子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

exec = require('child_process').exec;

child = exec('node child.js');
child.stdout.pipe(process.stdout);
child.kill('SIGKILL');

function wait() {
    setTimeout(wait, 1000);
    child.kill('SIGKILL');
}
wait();

上面的代码不起作用.孩子开始并将无限期地继续写入输出.我不知道如何杀死这个子进程.我在 Windows 7 中运行 node v0.11.9.我知道 Windows 不使用 POSIX 信号,但发送它WM_QUIT"会导致异常.我在标准输入上设置事件协议的最佳解决方案是什么?

The above code does not work. The child starts and will continue to write output indefinitely. I can not figure out how to kill this child process. I am running node v0.11.9 in Windows 7. I know that Windows does not use POSIX signals but sending it 'WM_QUIT' results in an exception. Is my best solution to setup an event protocol on stdin?

推荐答案

对于当前接受的答案,这仍然对我不起作用.您可以在 Windows 上使用的解决方法是调用 windows taskkill 程序为您终止子进程.不太好,但它的工作原理.当您生成子进程时,您会在生成时返回的子对象中获得一个 ProcessID (pid),您可以使用 with taskkill 来终止进程.

This still doesn't work for me with the current accepted answer. A work around on windows you can use is to call upon the windows taskkill program to kill the child process for you. Not quite as nice but it works. When you spawn the child you get a ProcessID (pid) stored in the child object returned when spawning, you can use with taskkill to kill the process.

var spawn = require('child_process').spawn;    
spawn("taskkill", ["/pid", child.pid, '/f', '/t']);

这篇关于为什么我不能在 Windows 上的 nodejs 中杀死我的子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆