4

I run CMD to spawn, but if you'll send me a ping command, I can not get out of it, how can I send the console control + c, to avoid this? THANKS!

var fs = require('fs');
var iconv = require('iconv-lite');
function sendData (msg) {
    console.log('write msg ', msg);
    cmd.stdin.write(msg + "\r\n");
}
function execCommand() {
    console.log('start command line')
    var s = { 
        e : 'exec_command',
        d : {
          data : {}
        }
    };
    cmd = require('child_process').spawn('cmd', ['/K']);

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

}

execCommand();
sendData('ping e1.ru -t');
sendData( EXIT ??? )

????? I want to make a console, a full-fledged console through node.js.

sendData('dir');
sendData('cd /d Windows');
sendData('ping 8.8.8.8 -t');
senData( CONTROL + C );
senData('dir')
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Yaroslav L.
  • 585
  • 2
  • 7
  • 17

1 Answers1

3

You'll want to explicitly call:

cmd.kill();

that'll do the trick. If you require the equivalent of CTRL-C then call:

cmd.kill('SIGINT');

See child_process.kill docs for more info.

JP Richardson
  • 38,609
  • 36
  • 119
  • 151
  • 3
    NO KILL PROCESS! I HAVE TO MAKE A STOP PING, NO KILL SPAWN. – Yaroslav L. May 02 '13 at 09:15
  • Are you expecting to send more commands through the child process? – booyaa May 02 '13 at 10:05
  • This is not completely exact. If the process did a `stdin` redirection, like `std:cin` and `std::cout << " " << data;`, that keeps the `stdin` open, sending a `child.kill('SIGINT')`will not work. NOTE. The `child.stdin.write('"\x03")` will not work as well. – loretoparisi May 04 '17 at 18:30