我正在尝试将子进程的stdout / stderr传递给特定的终端/ TTY。
这是我的代码:
//start
if ($fd !== undefined) {
try {
fs.closeSync($fd)
}
catch (err) {
console.error(err.stack || err);
}
}
$fd = fs.openSync(fd_stdout, 'a');
child = cp.spawn(executable, execStringArray, { });
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
const strm = fs.createWriteStream(projectWatcherOutputLogPath);
child.stdout.pipe(strm);
child.stderr.pipe(strm);
if (tty.isatty($fd)) {
console.error('fd with value => ', $fd, '*is* a tty!');
const stdoutStrm = fs.createWriteStream(null, { fd: $fd });
child.stdout.pipe(stdoutStrm);
}
else {
console.error('fd with value => ', $fd, 'is not a tty');
}
if (tty.isatty($fd)) {
console.error('fd with value => ', $fd, '*is* a tty!');
const stderrStrm = fs.createWriteStream(null, { fd: $fd });
child.stderr.pipe(stderrStrm);
}
else {
console.error('fd with value => ', $fd, 'is not a tty');
}
child.on('close', function () {
console.log(' => Suman server => project-watcher child process has fired "close" event.');
});
//end
在这次工作之后会发生什么,我收到一个错误:
EBADF: bad file descriptor, close
除此之外没有其他堆栈跟踪,即使我使用节点execArgv '--trace-warnings'
。
也许我需要在打开文件后关闭它?