完成时的子进程事件

Child process events when finish

有一种方法可以扩展 child_process nodejs 的事件 似乎有时没有调用事件,所以我想看看进程是否结束然后触发事件。 例如,我使用 spawn 并且进程 succeeded 但没有调用以下事件,所以我可以通过事件或其他方式跟踪 spawn 进程完成时的情况吗?

function childProcessPromise(mtd, cmd, options) {
    var child = child_process.spawn(cmd, value, opt);
    child.stdout.on('data', function (data) {
        console.log('data' + data);
    });
    child.stderr.on('data', function (data) {
        console.log('test: ' + data);
        reject(data);
    });
    child.on('close', function (code) {
        console.log("close");
    });
    child.on('error', function (err) {
        console.log(err);
    });

  });

}

文档表明您所观察到的可能是正确的:

https://nodejs.org/api/child_process.html#child_process_event_close

事件:'close'#

code Number the exit code if the child exited on its own. signal String the signal by which the child process was terminated. The 'close' event is emitted when the stdio streams of a child process have been closed. This is distinct from the 'exit' event, since multiple processes might share the same stdio streams.

由于默认情况下子流与父流共享,因此只要父流打开,流就不会关闭

https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

我对 spawn 没有什么经验,也没有亲眼见过这个,但这就是我解释文档的方式