Node.js: How to send control C to child process

让人想犯罪 __ 提交于 2019-12-12 02:08:39

问题


I am writing one web-like linux shell using node.js + socket.io. Simple command like, ls, cd are working well. But when issue command like ping google.com, the stdout is printing endlessly. I tried to send Ctrl +C to stdin, but no luck.

1) spawn 'bash' process

spawn = require('child_process').spawn;
var sh = spawn('bash');

2) send bash stdout to socket.io

sh.stdout.on('data', function(data) {
   console.log('stdout' + data);
   listener.sockets.emit("stdout",new Buffer(data));
});

3) Sending Ctl C (\x03) to bash's stdin. var listener = io.listen(server);

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));
   });
});

I am stuck at this point. Seems like


回答1:


Try process.kill(sh.pid). I use this to kill workers in a cluster when my master process shuts down. sh.signalCode should be equal to SIGTERM. Of course, I have no idea if this works on Windows.



来源:https://stackoverflow.com/questions/11696967/node-js-how-to-send-control-c-to-child-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!