我正在使用node.js + socket.io编写一个类似web的linux shell。ls、cd等简单命令运行良好。但是当发出像ping google.com这样的命令时,标准输出就会被无休止地打印出来。我尝试将Ctrl +C发送到stdin,但没有成功。
1)产生'bash‘进程
spawn = require('child_process').spawn;
var sh = spawn('bash');2)发送bash stdout到socket.io
sh.stdout.on('data', function(data) {
console.log('stdout' + data);
listener.sockets.emit("stdout",new Buffer(data));
});3)将Ctl C (\x03)发送到bash的stdin。var listener =io.listen(服务端);
listener.set('log level',1);
listener.sockets.on('connection', function(client){
client.on('message', function(data){
if(data === "KILL") {
console.log('!!!!' + data);
sh.stdin.write('\x03');
client.broadcast.send(new Buffer("KILLING "));
//return;
};
console.log(data);
sh.stdin.write(data+"\n");
client.broadcast.send(new Buffer("> "+data));
});
});我被困在这一点上了。看起来像
发布于 2012-07-28 08:18:13
试试process.kill(sh.pid)。当我的主进程关闭时,我使用它来杀死集群中的工作进程。sh.signalCode应等于SIGTERM。当然,我不知道这在Windows上是否有效。
https://stackoverflow.com/questions/11696967
复制相似问题