相关疑难解决方法(0)

如何发送控制 C node.js 和 child_processes

你好我要发送给child_process,比如ping 8.8.8.8-t,也就是无限次ping。有些迭代我想停止这个命令并执行一个新的,但在这种情况下我不想杀死一个子进程。

例子:

var spawn = require('child_process').spawn('cmd'),
    iconv = require('iconv-lite');

spawn.stdout.on('data', function (data) {
    console.log('Stdout: ', iconv.decode(data, 'cp866'));
});

spawn.stderr.on('data', function (data) {
    console.log('Stderr: ', iconv.decode(data, 'cp866'));
});

spawn.stdin.write('ping 8.8.8.8 -t'+ '\r\n');

spawn.stdin.write(here control-c...); // WRONG

spawn.stdin.write('dir' + '\r\n');
Run Code Online (Sandbox Code Playgroud)

windows child-process node.js

5
推荐指数
1
解决办法
4478
查看次数

如何在 Node.js 中向子进程发送“CTRL+C”?

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

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);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,子进程被停止。我也尝试vcdGen.stdin.write('\x03')过,vcdGen.kill('SIGINT');但它不起作用。

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

windows spawn sigint node.js

3
推荐指数
1
解决办法
4418
查看次数

标签 统计

node.js ×2

windows ×2

child-process ×1

sigint ×1

spawn ×1