使用 stdin.write() 将 crtl+c 发送到 node.js 生成的子进程



在节点脚本中,我生成了一个执行批处理文件运行的子进程.bat ,要终止由批处理文件启动的程序,我需要将 ctrl+c 组合发送到子进程,我需要使用 stdin.write() 方法将 ctrl+c 组合发送到程序。

var hmc = require('child_process').spawn('cmd');
hmc.stdin.write('run.bat n');

CTRL+C 等效于在 Windows 上发送SIGINT。您可以改为发送信号,而不是尝试向进程发送击键。这可以使用子进程方法或其他进程完成,前提是您具有子进程 ID:

hmc.kill('SIGINT');
// or from another process
process.kill(hmc.pid, 'SIGINT');

最新更新