options.stdio


options.stdio 选项用于配置在父进程和子进程之间建立的管道。 默认情况下,子进程的 stdin、 stdout 和 stderr 被重定向到 ChildProcess 对象上的相应 subprocess.stdinsubprocess.stdoutsubprocess.stderr 流。 这相当于将 options.stdio 设置为 ['pipe', 'pipe', 'pipe']

为方便起见, options.stdio 可以是以下字符串之一:

  • 'pipe' - 相当于 ['pipe', 'pipe', 'pipe'](默认值)。
  • 'ignore' - 相当于 ['ignore', 'ignore', 'ignore']
  • 'inherit' - 相当于 ['inherit', 'inherit', 'inherit'][0, 1, 2]

否则, options.stdio 的值是一个数组,其中每个索引对应于子进程中的 fd。 fd 0、1 和 2 分别对应于 stdin、stdout 和 stderr。 可以指定其他 fd 以在父进程和子进程之间创建其他管道。 值为以下之一:

  1. 'pipe' - 在子进程和父进程之间创建一个管道。 管道的父端作为 child_process 对象上的属性 subprocess.stdio[fd] 暴露给父进程。 为 fd 0 - 2 创建的管道也可分别作为 subprocess.stdinsubprocess.stdoutsubprocess.stderr 使用。
  1. 'ipc' - 创建一个 IPC 通道,用于在父进程和子进程之间传递消息或文件描述符。 一个 ChildProcess 最多可以有一个 IPC stdio 文件描述符。 设置此选项将启用 subprocess.send() 方法。 如果子进程是 Node.js 进程,则 IPC 通道的存在将启用 process.send()process.disconnect() 方法、以及子进程内的 'disconnect''message' 事件。

    不支持以 process.send() 以外的任何方式访问 IPC 通道 fd,或者使用不具有 Node.js 实例的子进程使用 IPC 通道。

  2. 'ignore' - 指示 Node.js 忽略子进程中的 fd。 虽然 Node.js 将始终为它衍生的进程打开 fd 0 - 2,但将 fd 设置为 'ignore' 将导致 Node.js 打开 /dev/null 并将其附加到子进程的 fd。

  3. 'inherit' - 将相应的 stdio 流传给父进程或从父进程传入。 在前三个位置,这分别相当于 process.stdinprocess.stdoutprocess.stderr。 在任何其他位置则相当于 'ignore'

  4. <Stream> 对象 - 与子进程共享指向 tty、文件、套接字或管道的可读或可写流。 流的底层文件描述符在子进程中复制到与 stdio 数组中的索引对应的 fd。 注意,流必须具有底层描述符(文件流直到触发 'open' 事件才需要)。

  5. 正整数 - 整数值被解释为当前在父进程中打开的文件描述符。 它与子进程共享,类似于共享 <Stream> 对象。

  6. nullundefined - 使用默认值。 对于 stdio 的 fd 0、1 和 2(换句话说,stdin、stdout 和 stderr),将创建一个管道。 对于 fd 3 及更高版本,默认为 'ignore'

const { spawn } = require('child_process');

// 子进程使用父进程的 stdio。
spawn('prg', [], { stdio: 'inherit' });

// 衍生的子进程只共享 stderr。
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });

// 打开一个额外的 fd=4,与呈现启动式界面的程序进行交互。
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });

注意,当在父进程和子进程之间建立 IPC 通道,并且子进程是 Node.js 进程时,子进程在未指向(使用 unref())IPC 通道的情况下启动直到子进程注册事件处理函数用于 'disconnect' 事件或 'message' 事件。 这允许子进程正常退出而不需要通过开放的 IPC 通道保持打开该进程。

在类 UNIX 操作系统上,child_process.spawn() 方法在将事件循环与子进程解耦之前会同步地执行内存操作。 具有大内存占用的应用程序可能会发现频繁的 child_process.spawn() 调用成为瓶颈。 有关更多信息,参阅 V8 问题 7381

还可参阅:child_process.exec()child_process.fork()

The options.stdio option is used to configure the pipes that are established between the parent and child process. By default, the child's stdin, stdout, and stderr are redirected to corresponding subprocess.stdin, subprocess.stdout, and subprocess.stderr streams on the ChildProcess object. This is equivalent to setting the options.stdio equal to ['pipe', 'pipe', 'pipe'].

For convenience, options.stdio may be one of the following strings:

  • 'pipe' - equivalent to ['pipe', 'pipe', 'pipe'] (the default)
  • 'ignore' - equivalent to ['ignore', 'ignore', 'ignore']
  • 'inherit' - equivalent to ['inherit', 'inherit', 'inherit'] or [0, 1, 2]

Otherwise, the value of options.stdio is an array where each index corresponds to an fd in the child. The fds 0, 1, and 2 correspond to stdin, stdout, and stderr, respectively. Additional fds can be specified to create additional pipes between the parent and child. The value is one of the following:

  1. 'pipe' - Create a pipe between the child process and the parent process. The parent end of the pipe is exposed to the parent as a property on the child_process object as subprocess.stdio[fd]. Pipes created for fds 0 - 2 are also available as subprocess.stdin, subprocess.stdout and subprocess.stderr, respectively.

  2. 'ipc' - Create an IPC channel for passing messages/file descriptors between parent and child. A ChildProcess may have at most one IPC stdio file descriptor. Setting this option enables the subprocess.send() method. If the child is a Node.js process, the presence of an IPC channel will enable process.send() and process.disconnect() methods, as well as 'disconnect' and 'message' events within the child.

    Accessing the IPC channel fd in any way other than process.send() or using the IPC channel with a child process that is not a Node.js instance is not supported.

  3. 'ignore' - Instructs Node.js to ignore the fd in the child. While Node.js will always open fds 0 - 2 for the processes it spawns, setting the fd to 'ignore' will cause Node.js to open /dev/null and attach it to the child's fd.

  4. 'inherit' - Pass through the corresponding stdio stream to/from the parent process. In the first three positions, this is equivalent to process.stdin, process.stdout, and process.stderr, respectively. In any other position, equivalent to 'ignore'.

  5. <Stream> object - Share a readable or writable stream that refers to a tty, file, socket, or a pipe with the child process. The stream's underlying file descriptor is duplicated in the child process to the fd that corresponds to the index in the stdio array. Note that the stream must have an underlying descriptor (file streams do not until the 'open' event has occurred).

  6. Positive integer - The integer value is interpreted as a file descriptor that is currently open in the parent process. It is shared with the child process, similar to how <Stream> objects can be shared.

  7. null, undefined - Use default value. For stdio fds 0, 1, and 2 (in other words, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the default is 'ignore'.

const { spawn } = require('child_process');

// Child will use parent's stdios
spawn('prg', [], { stdio: 'inherit' });

// Spawn child sharing only stderr
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });

// Open an extra fd=4, to interact with programs presenting a
// startd-style interface.
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });

It is worth noting that when an IPC channel is established between the parent and child processes, and the child is a Node.js process, the child is launched with the IPC channel unreferenced (using unref()) until the child registers an event handler for the 'disconnect' event or the 'message' event. This allows the child to exit normally without the process being held open by the open IPC channel.

On UNIX-like operating systems, the child_process.spawn() method performs memory operations synchronously before decoupling the event loop from the child. Applications with a large memory footprint may find frequent child_process.spawn() calls to be a bottleneck. For more information, see V8 issue 7381.

See also: child_process.exec() and child_process.fork().