我正在使用Node的spawn来创建一个新进程.有时,此过程将很快退出,并在stderr上显示错误代码和消息.似乎stderr在这个快速转变中迷失了方向.我试过这个: reader.stderr.on('data', function (buf) {
reader.stderr.on('data', function (buf) { console.log('stderr message: ' + buf); }); reader.on('exit', function (code, signal) { console.log('Exit'); });
输出:
Exit stderr message: ERROR: Missing required option for command.
我也尝试在退出监听器中读取它,但没有运气:
reader.on('exit', function (code, signal) { console.log('Exit'); console.log('stderr: ' + reader.stderr.read()); });
输出:
Exit stderr: null
因此,问题似乎是stderr输出太慢,并且在我需要该信息的退出事件之后很晚.我怎样才能解决这个问题?
取自 child_process docs forexit
:
Note that the child process stdio streams might still be open.
然后他们描述了近距离事件:
This event is emitted when the stdio streams of a child process have all terminated. This is distinct from ‘exit’, since multiple processes might share the same stdio streams.
所以看起来你应该使用close,而不是退出.