我一直在查看文档,但我找不到一种方法将命令传递到命令行 (cmd.exe(,而无需在最后关闭它。如何"持续"传达命令行?
我注意到了分叉功能,但它似乎仅适用于节点.js模块。
const { spawn } = require('child_process');
const ls = spawn('C:\Windows\System32\cmd', ['/c', 'echo hello']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
结果:
stdout: hello
child process exited with code 0 // I dont want it to exit yet!
最后这是相当微不足道的。只需生成程序并写入其stdin
:
const { spawn } = require('child_process');
const child = spawn('C:\Windows\System32\cmd.exe');
child.stdin.write('echo IM BACKn');
child.stdout.on('data', (data) => {
console.log(`command line says:n${data}`);
});
生产:
c:Users... 3 cmd.exe communcation>
command line says:
echo IM BACK
IM BACK
还可以添加"回声关闭">