事件:'close'
¥Event: 'close'
-
code
<number> 如果子进程自行退出,则返回退出代码;如果子进程因信号终止,则返回null
。¥
code
<number> The exit code if the child process exited on its own, ornull
if the child process terminated due to a signal. -
signal
<string> 终止子进程的信号,如果子进程不是由于信号而终止,则为null
。¥
signal
<string> The signal by which the child process was terminated, ornull
if the child process did not terminated due to a signal.
'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 process failed to spawn.
如果进程退出,则 code
为进程的最终退出码,否则为 null
。如果进程因收到信号而终止,则 signal
是信号的字符串名称,否则为 null
。两者之一将始终是非 null
。
¥If the process exited, code
is the final exit code of the process, otherwise
null
. If the process terminated due to receipt of a signal, signal
is the
string name of the signal, otherwise null
. One of the two will always be
non-null
.
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}`);
});
import { spawn } from '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}`);
});