事件:'close'
🌐 Event: 'close'
'close' 事件在一个进程结束并且子进程的 stdio 流已经关闭之后触发。这与 'exit' 事件不同,因为多个进程可能共享相同的 stdio 流。'close' 事件总是在 'exit' 已经触发之后,或者在子进程未能启动时触发 'error' 之后发出。
🌐 The 'close' event is emitted after a process has ended and 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. The 'close' event will always emit after 'exit' was
already emitted, or 'error' if the child failed to spawn.
const { spawn } = require('node:child_process');
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
});
ls.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
});