如何发送“CTRL+C"Node.js 中的子进程? [英] How to send "CTRL+C" to child process in Node.js?

查看:44
本文介绍了如何发送“CTRL+C"Node.js 中的子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成子进程 - vvp (https://linux.die.net/人/1/vvp).在某些时候,我需要将 CTRL+C 发送到该进程.我希望模拟会被中断,并且我会收到交互式提示.之后我可以通过向子进程发送命令来继续模拟.所以,我尝试了这样的事情:

I tried to spawn child process - vvp (https://linux.die.net/man/1/vvp). At the certain time, I need to send CTRL+C to that process. I am expecting that simulation will be interrupted and I get the interactive prompt. And after that I can continue the simulation by send command to the child process. So, I tried something like this:

var child = require('child_process');
var fs = require('fs');
var vcdGen = child.spawn('vvp', ['qqq'], {});

vcdGen.stdout.on('data', function(data) {
  console.log(data.toString())
});

setTimeout(function() {
  vcdGen.kill('SIGINT');
}, 400);

在那种情况下,一个子进程被停止了.我也试过 vcdGen.stdin.write('\x03') 而不是 vcdGen.kill('SIGINT'); 但它不起作用.

In that case, a child process was stopped. I also tried vcdGen.stdin.write('\x03') instead of vcdGen.kill('SIGINT'); but it isn't work.

也许是因为 Windows?有什么方法可以实现与我在 cmd 中获得的行为相同的行为吗?

Maybe it's because of Windows? Is there any way to achieve the same behaviour as I got in cmd?

推荐答案

kill 仅真正支持 Windows 上的粗鲁进程终止 - Windows 中的应用程序信号模型和 *nix 不兼容.您不能通过标准输入传递 Ctrl+C ,因为它永远不会通过标准输入传递 - 它是控制台子系统的功能(因此您只能在进程具有附加控制台时使用它).它在子进程中创建一个新线程来完成它的工作.

kill only really supports a rude process kill on Windows - the application signal model in Windows and *nix isn't compatible. You can't pass Ctrl+C through standard input, because it never comes through standard input - it's a function of the console subsystem (and thus you can only use it if the process has an attached console). It creates a new thread in the child process to do its work.

没有支持以编程方式执行此操作的方法.这是用户的功能,而不是应用程序.做到这一点的唯一方法是做与控制台子系统相同的事情 - 在目标应用程序中创建一个新线程并让它执行信令.但最好的方法是简单地使用协同信号——尽管这当然需要您更改目标应用程序以理解信号.

There's no supported way to do this programmatically. It's a feature for the user, not the applications. The only way to do this would be to do the same thing the console subsystem does - create a new thread in the target application and let it do the signalling. But the best way would be to simply use coöperative signalling instead - though that of course requires you to change the target application to understand the signal.

如果您想走完全不受支持的路线,请查看 https://stackoverflow.com/a/1179124/3032289.

If you want to go the entirely unsupported route, have a look at https://stackoverflow.com/a/1179124/3032289.

如果你想找到一个中间立场,当然有一种方法可以向你自己发送信号.这也意味着如果您的控制台已连接,您可以将 Ctrl+C 发送到进程.毋庸置疑,这非常棘手 - 您可能想要创建一个本机主机进程,它除了创建控制台并运行您想要运行的实际程序之外什么都不做.然后,您的主机进程将侦听事件,并在发出事件信号时调用 GenerateConsoleCtrlEvent.

If you want to find a middle ground, there's a way to send a signal to yourself, of course. Which also means that you can send Ctrl+C to a process if your consoles are attached. Needless to say, this is very tricky - you'd probably want to create a native host process that does nothing but create a console and run the actual program you want to run. Your host process would then listen for an event, and when the event is signalled, call GenerateConsoleCtrlEvent.

这篇关于如何发送“CTRL+C"Node.js 中的子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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