你好我要发送给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) 我尝试生成子进程 - 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 中获得的相同的行为吗?