Yaroslav L.
Yaroslav L.

Reputation: 595

How to send CONTROL+C at spawn in nodejs

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')

Upvotes: 5

Views: 4282

Answers (1)

JP Richardson
JP Richardson

Reputation: 39395

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.

Upvotes: 3

Related Questions