windows - How to send control C node.js and child_processes -


hello want send child_process, example, ping 8.8.8.8-t, is, infinite number of ping. , of iterations want stop command , execute new, in case not want kill child process.

example:

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

i found previous question. looks trying create/emulate terminal within node.js. can use readline reading , writing terminal.

to write control character, can see example docs :

  rl.write('delete me!');   // simulate ctrl+u delete line written   rl.write(null, {ctrl: true, name: 'u'}); 

to directly answer question, pass special characters need pass ascii values. ctrl + c becomes ascii character 0x03. value taken here.

  spawn.stdin.write("\x03"); 

Comments