0

In a node script, I have spawned a child process which executes a batch file run.bat , to terminate the program started by the batch-file i need to send ctrl+c combination to the child process , it is required for me to send ctrl+c combination to the program using stdin.write() method.

var hmc = require('child_process').spawn('cmd');
hmc.stdin.write('run.bat \n');
shadow0wolf
  • 111
  • 2
  • 15

1 Answers1

1

A CTRL+C is equivalent to sending a SIGINT on Windows. Rather than trying to send a keystroke to the process, you can send a signal instead. This can either be done with a child process method or from other processes, provided that you have the process ID of the child:

hmc.kill('SIGINT');
// or from another process
process.kill(hmc.pid, 'SIGINT');
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
  • 1
    https://nodejs.org/api/process.html#process_signal_events Note that Windows does not support sending Signals, but node offers some emulation with process.kill(), and child_process.kill(): - Sending signal 0 can be used to search for the existence of a process - Sending SIGINT, SIGTERM, and SIGKILL cause the unconditional exit of the target process. – garkin Sep 03 '15 at 09:08
  • 1
    So it wont be a signal in windows. You will just kill the process immediately (not gracefully). – garkin Sep 03 '15 at 09:09
  • @garkin's comment is right, the signal is NOT sent, but rather the child process is just unconditionally killed. – RocketMan Aug 20 '20 at 18:32