【发布时间】:2019-10-24 13:32:27
【问题描述】:
我可以在 node.js 中使用子进程和 spawn 进行 cmd,我希望将此命令的输出写入文件而不是 stdout。
test.js
const expect = require('chai').expect;
const { spawn } = require('child_process')
let path = require('path');
let fs = require('fs');
//tried but didn't work
1) const cmd = spawn(ansysfnonetclient, options, {
stdio: [
0, // Use parent's stdin for child.
'pipe', // Pipe child's stdout to parent.
fs.openSync('err.out', 'w') // Direct child's stderr to a file.
]
});
2) const cmd = spawn(ansysfnonetclient, options, {shell: true, stdio: 'inherit'});
it('run the cmd and write o/p to file', function (done) {
this.timeout(30000);
let options = ['-h','-o','temp.log'];
let ansysfnonetclient = path.resolve(__dirname,'../../../../../../../../saoptest/annetclient.exe');
const cmd = spawn(ansysfnonetclient, options, {shell: true, stdio: 'inherit'});
console.log(cmd);
done();
});
【问题讨论】:
-
不行吗?
let options = ['-h', '>', 'temp.log'];@Justin 这是怎么重复的?他想知道他如何在文件中获取子进程的输出,而不是如何在文件中写入 -
感谢您的回复,.exe 的第一个命令不是内置命令,它只能采用 -h 选项,我如何将其写入文件,我将提交作为答案
标签: javascript node.js testing npm child-process