options.detached
在 Windows 上,将 options.detached 设置为 true 可以让子进程在父进程退出后继续运行。子进程将拥有自己的控制台窗口。一旦为子进程启用此选项,就无法禁用。
【On Windows, setting options.detached to true makes it possible for the
child process to continue running after the parent exits. The child process
will have its own console window. Once enabled for a child process,
it cannot be disabled.】
在非 Windows 平台上,如果将 options.detached 设置为 true,子进程将成为新进程组和会话的领导者。无论子进程是否被分离,父进程退出后子进程都可能继续运行。有关更多信息,请参阅 setsid(2)。
【On non-Windows platforms, if options.detached is set to true, the child
process will be made the leader of a new process group and session. Child
processes may continue running after the parent exits regardless of whether
they are detached or not. See setsid(2) for more information.】
默认情况下,父进程会等待已分离的子进程退出。要防止父进程等待某个 subprocess 退出,可以使用 subprocess.unref() 方法。这样做会导致父进程的事件循环不将子进程计入其引用计数,从而允许父进程独立于子进程退出,除非子进程与父进程之间存在已建立的进程间通信(IPC)通道。
【By default, the parent will wait for the detached child process to exit.
To prevent the parent process from waiting for a given subprocess to exit, use
the subprocess.unref() method. Doing so will cause the parent process' event
loop to not include the child process in its reference count, allowing the
parent process to exit independently of the child process, unless there is an established
IPC channel between the child and the parent processes.】
在使用 detached 选项启动一个长时间运行的进程时,除非为其提供了不与父进程连接的 stdio 配置,否则父进程退出后,该进程不会在后台继续运行。如果父进程的 stdio 被继承,子进程将继续附着在控制终端上。
【When using the detached option to start a long-running process, the process
will not stay running in the background after the parent exits unless it is
provided with a stdio configuration that is not connected to the parent.
If the parent process' stdio is inherited, the child process will remain attached
to the controlling terminal.】
一个长时间运行的进程示例,通过分离并忽略其父进程的 stdio 文件描述符,以忽略父进程的终止:
【Example of a long-running process, by detaching and also ignoring its parent
stdio file descriptors, in order to ignore the parent's termination:】
const { spawn } = require('node:child_process');
const process = require('node:process');
const subprocess = spawn(process.argv[0], ['child_program.js'], {
detached: true,
stdio: 'ignore',
});
subprocess.unref();import { spawn } from 'node:child_process';
import process from 'node:process';
const subprocess = spawn(process.argv[0], ['child_program.js'], {
detached: true,
stdio: 'ignore',
});
subprocess.unref();或者,可以将子进程的输出重定向到文件中:
【Alternatively one can redirect the child process' output into files:】
const { openSync } = require('node:fs');
const { spawn } = require('node:child_process');
const out = openSync('./out.log', 'a');
const err = openSync('./out.log', 'a');
const subprocess = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ],
});
subprocess.unref();import { openSync } from 'node:fs';
import { spawn } from 'node:child_process';
const out = openSync('./out.log', 'a');
const err = openSync('./out.log', 'a');
const subprocess = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ],
});
subprocess.unref();