<spanclass="hljs-built_in">console</span>.log(<spanclass="hljs-string">`child process exited with code <spanclass="hljs-subst">${code}</span>`</span>);
<p>By default, pipes for <code>stdin</code>, <code>stdout</code>, and <code>stderr</code> are established between
the parent Node.js process and the spawned child. These pipes have
limited (and platform-specific) capacity. If the child process writes to
stdout in excess of that limit without the output being captured, the child
process will block waiting for the pipe buffer to accept more data. This is
identical to the behavior of pipes in the shell. Use the <code>{ stdio: 'ignore' }</code>
option if the output will not be consumed.</p>
<p>The command lookup will be performed using <code>options.env.PATH</code> environment
variable if passed in <code>options</code> object, otherwise <code>process.env.PATH</code> will be
used. To account for the fact that Windows environment variables are
case-insensitive Node.js will lexicographically sort all <code>env</code> keys and choose
the first one case-insensitively matching <code>PATH</code> to perform command lookup.
This may lead to issues on Windows when passing objects to <code>env</code> option that
have multiple variants of <code>PATH</code> variable.</p>
<p>The <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> method spawns the child process asynchronously,
without blocking the Node.js event loop. The <ahref="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>
function provides equivalent functionality in a synchronous manner that blocks
the event loop until the spawned process either exits or is terminated.</p>
<p>For convenience, the <code>child_process</code> module provides a handful of synchronous
and asynchronous alternatives to <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> and
<ahref="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>. Each of these alternatives are implemented on
top of <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> or <ahref="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>.</p>
<ul>
<li><ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>: spawns a shell and runs a command within that
shell, passing the <code>stdout</code> and <code>stderr</code> to a callback function when
complete.</li>
<li><ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a>: similar to <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> except
that it spawns the command directly without first spawning a shell by
default.</li>
<li><ahref="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>: spawns a new Node.js process and invokes a
specified module with an IPC communication channel established that allows
sending messages between parent and child.</li>
<li><ahref="#child_process_child_process_execsync_command_options"><code>child_process.execSync()</code></a>: a synchronous version of
<ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> that will block the Node.js event loop.</li>
<li><ahref="#child_process_child_process_execfilesync_file_args_options"><code>child_process.execFileSync()</code></a>: a synchronous version of
<ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> that will block the Node.js event loop.</li>
</ul>
<p>For certain use cases, such as automating shell scripts, the
<ahref="#child_process_synchronous_process_creation">synchronous counterparts</a> may be more convenient. In many cases, however,
the synchronous methods can have significant impact on performance due to
stalling the event loop while spawned processes complete.</p>
<h2>Asynchronous process creation<span><aclass="mark"href="#child_process_asynchronous_process_creation"id="child_process_asynchronous_process_creation">#</a></span></h2>
and <ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> methods all follow the idiomatic asynchronous
programming pattern typical of other Node.js APIs.</p>
<p>Each of the methods returns a <ahref="#child_process_child_process"><code>ChildProcess</code></a> instance. These objects
implement the Node.js <ahref="events.html#events_class_eventemitter"><code>EventEmitter</code></a> API, allowing the parent process to
register listener functions that are called when certain events occur during
the life cycle of the child process.</p>
<p>The <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> and <ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> methods
additionally allow for an optional <code>callback</code> function to be specified that is
invoked when the child process terminates.</p>
<h3>Spawning <code>.bat</code> and <code>.cmd</code> files on Windows<span><aclass="mark"href="#child_process_spawning_bat_and_cmd_files_on_windows"id="child_process_spawning_bat_and_cmd_files_on_windows">#</a></span></h3>
<p>The importance of the distinction between <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> and
<ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> can vary based on platform. On Unix-type
operating systems (Unix, Linux, macOS) <ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> can be
more efficient because it does not spawn a shell by default. On Windows,
however, <code>.bat</code> and <code>.cmd</code> files are not executable on their own without a
terminal, and therefore cannot be launched using <ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a>.
When running on Windows, <code>.bat</code> and <code>.cmd</code> files can be invoked using
<ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> with the <code>shell</code> option set, with
<ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>, or by spawning <code>cmd.exe</code> and passing the <code>.bat</code> or
<code>.cmd</code> file as an argument (which is what the <code>shell</code> option and
<ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> do). In any case, if the script filename contains
<li><code>command</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The command to run, with space-separated arguments.</li>
<li><code>cwd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Current working directory of the child process.
<li><code>shell</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Shell to execute the command with. See
<ahref="#child_process_shell_requirements">Shell requirements</a> and <ahref="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong>
<li><code>maxBuffer</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Largest amount of data in bytes allowed on stdout or
stderr. If exceeded, the child process is terminated and any output is
truncated. See caveat at <ahref="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>.
<li><code>uid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the user identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li>
<li><code>gid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the group identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li>
<li><code>windowsHide</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Hide the subprocess console window that would
normally be created on Windows systems. <strong>Default:</strong><code>false</code>.</li>
<li><code>callback</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function"class="type"><Function></a> called with the output when process terminates.
<p>If <code>timeout</code> is greater than <code>0</code>, the parent will send the signal
identified by the <code>killSignal</code> property (the default is <code>'SIGTERM'</code>) if the
child runs longer than <code>timeout</code> milliseconds.</p>
<p>Unlike the <ahref="http://man7.org/linux/man-pages/man3/exec.3.html"><code>exec(3)</code></a> POSIX system call, <code>child_process.exec()</code> does not replace
the existing process and uses a shell to execute the command.</p>
<p>If this method is invoked as its <ahref="util.html#util_util_promisify_original"><code>util.promisify()</code></a>ed version, it returns
a <code>Promise</code> for an <code>Object</code> with <code>stdout</code> and <code>stderr</code> properties. The returned
<code>ChildProcess</code> instance is attached to the <code>Promise</code> as a <code>child</code> property. In
case of an error (including any error resulting in an exit code other than 0), a
rejected promise is returned, with the same <code>error</code> object given in the
callback, but with two additional properties <code>stdout</code> and <code>stderr</code>.</p>
<li><code>file</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The name or path of the executable file to run.</li>
<li><code>args</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string[]></a> List of string arguments.</li>
<li><code>cwd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Current working directory of the child process.</li>
<li><code>maxBuffer</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Largest amount of data in bytes allowed on stdout or
stderr. If exceeded, the child process is terminated and any output is
truncated. See caveat at <ahref="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>.
<li><code>uid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the user identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li>
<li><code>gid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the group identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li>
<li><code>windowsHide</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Hide the subprocess console window that would
normally be created on Windows systems. <strong>Default:</strong><code>false</code>.</li>
<li><code>windowsVerbatimArguments</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> No quoting or escaping of arguments is
done on Windows. Ignored on Unix. <strong>Default:</strong><code>false</code>.</li>
<li><code>shell</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
<li><code>callback</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function"class="type"><Function></a> Called with the output when process terminates.
<p>The <code>child_process.execFile()</code> function is similar to <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>
except that it does not spawn a shell by default. Rather, the specified
executable <code>file</code> is spawned directly as a new process making it slightly more
efficient than <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>.</p>
<p>The same options as <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> are supported. Since a shell is
not spawned, behaviors such as I/O redirection and file globbing are not
<li><code>modulePath</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The module to run in the child.</li>
<li><code>args</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string[]></a> List of string arguments.</li>
<li><code>cwd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Current working directory of the child process.</li>
<li><code>detached</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Prepare child to run independently of its parent
process. Specific behavior depends on the platform, see
<li><code>execPath</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Executable used to create the child process.</li>
<li><code>execArgv</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string[]></a> List of string arguments passed to the executable.
<li><code>serialization</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Specify the kind of serialization used for sending
messages between processes. Possible values are <code>'json'</code> and <code>'advanced'</code>.
<li><code>silent</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> If <code>true</code>, stdin, stdout, and stderr of the child will be
piped to the parent, otherwise they will be inherited from the parent, see
the <code>'pipe'</code> and <code>'inherit'</code> options for <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>'s
<ahref="#child_process_options_stdio"><code>stdio</code></a> for more details. <strong>Default:</strong><code>false</code>.</li>
<li><code>stdio</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"class="type"><Array></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> See <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>'s <ahref="#child_process_options_stdio"><code>stdio</code></a>.
When this option is provided, it overrides <code>silent</code>. If the array variant
is used, it must contain exactly one item with value <code>'ipc'</code> or an error
will be thrown. For instance <code>[0, 1, 2, 'ipc']</code>.</li>
<li><code>windowsVerbatimArguments</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> No quoting or escaping of arguments is
done on Windows. Ignored on Unix. <strong>Default:</strong><code>false</code>.</li>
<li><code>uid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the user identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li>
<li><code>gid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the group identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li>
<p>The <code>child_process.fork()</code> method is a special case of
<ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> used specifically to spawn new Node.js processes.
Like <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>, a <ahref="#child_process_child_process"><code>ChildProcess</code></a> object is returned. The
returned <ahref="#child_process_child_process"><code>ChildProcess</code></a> will have an additional communication channel
built-in that allows messages to be passed back and forth between the parent and
child. See <ahref="#child_process_subprocess_send_message_sendhandle_options_callback"><code>subprocess.send()</code></a> for details.</p>
<p>Keep in mind that spawned Node.js child processes are
independent of the parent with exception of the IPC communication channel
that is established between the two. Each process has its own memory, with
their own V8 instances. Because of the additional resource allocations
required, spawning a large number of child Node.js processes is not
recommended.</p>
<p>By default, <code>child_process.fork()</code> will spawn new Node.js instances using the
<ahref="process.html#process_process_execpath"><code>process.execPath</code></a> of the parent process. The <code>execPath</code> property in the
<code>options</code> object allows for an alternative execution path to be used.</p>
<p>Node.js processes launched with a custom <code>execPath</code> will communicate with the
parent process using the file descriptor (fd) identified using the
environment variable <code>NODE_CHANNEL_FD</code> on the child process.</p>
<p>Unlike the <ahref="http://man7.org/linux/man-pages/man2/fork.2.html"><code>fork(2)</code></a> POSIX system call, <code>child_process.fork()</code> does not clone the
current process.</p>
<p>The <code>shell</code> option available in <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> is not supported by
<code>child_process.fork()</code> and will be ignored if set.</p>
<li><code>command</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The command to run.</li>
<li><code>args</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string[]></a> List of string arguments.</li>
<li><code>cwd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Current working directory of the child process.</li>
<li><code>argv0</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Explicitly set the value of <code>argv[0]</code> sent to the child
process. This will be set to <code>command</code> if not specified.</li>
<li><code>stdio</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"class="type"><Array></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Child's stdio configuration (see
<li><code>detached</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Prepare child to run independently of its parent
process. Specific behavior depends on the platform, see
<li><code>uid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the user identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li>
<li><code>gid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the group identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li>
<li><code>serialization</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Specify the kind of serialization used for sending
messages between processes. Possible values are <code>'json'</code> and <code>'advanced'</code>.
<li><code>shell</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
<li><code>windowsVerbatimArguments</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> No quoting or escaping of arguments is
done on Windows. Ignored on Unix. This is set to <code>true</code> automatically
when <code>shell</code> is specified and is CMD. <strong>Default:</strong><code>false</code>.</li>
<li><code>windowsHide</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Hide the subprocess console window that would
normally be created on Windows systems. <strong>Default:</strong><code>false</code>.</li>
<spanclass="hljs-built_in">console</span>.log(<spanclass="hljs-string">`child process exited with code <spanclass="hljs-subst">${code}</span>`</span>);
<p>The <code>options.stdio</code> 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 <ahref="#child_process_subprocess_stdin"><code>subprocess.stdin</code></a>,
<ahref="#child_process_subprocess_stdout"><code>subprocess.stdout</code></a>, and <ahref="#child_process_subprocess_stderr"><code>subprocess.stderr</code></a> streams on the
<ahref="#child_process_child_process"><code>ChildProcess</code></a> object. This is equivalent to setting the <code>options.stdio</code>
equal to <code>['pipe', 'pipe', 'pipe']</code>.</p>
<p>For convenience, <code>options.stdio</code> may be one of the following strings:</p>
<ul>
<li><code>'pipe'</code>: equivalent to <code>['pipe', 'pipe', 'pipe']</code> (the default)</li>
<li><code>'ignore'</code>: equivalent to <code>['ignore', 'ignore', 'ignore']</code></li>
<li><code>'inherit'</code>: equivalent to <code>['inherit', 'inherit', 'inherit']</code> or <code>[0, 1, 2]</code></li>
</ul>
<p>Otherwise, the value of <code>options.stdio</code> 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:</p>
<ol>
<li>
<p><code>'pipe'</code>: 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
<code>child_process</code> object as <ahref="#child_process_subprocess_stdio"><code>subprocess.stdio[fd]</code></a>. Pipes
created for fds 0, 1, and 2 are also available as <ahref="#child_process_subprocess_stdin"><code>subprocess.stdin</code></a>,
<ahref="#child_process_subprocess_stdout"><code>subprocess.stdout</code></a> and <ahref="#child_process_subprocess_stderr"><code>subprocess.stderr</code></a>, respectively.</p>
</li>
<li>
<p><code>'ipc'</code>: Create an IPC channel for passing messages/file descriptors
between parent and child. A <ahref="#child_process_child_process"><code>ChildProcess</code></a> may have at most one IPC
stdio file descriptor. Setting this option enables the
<ahref="#child_process_subprocess_send_message_sendhandle_options_callback"><code>subprocess.send()</code></a> method. If the child is a Node.js process, the
presence of an IPC channel will enable <ahref="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a> and
<ahref="process.html#process_process_disconnect"><code>process.disconnect()</code></a> methods, as well as <ahref="process.html#process_event_disconnect"><code>'disconnect'</code></a> and
<ahref="process.html#process_event_message"><code>'message'</code></a> events within the child.</p>
<p>Accessing the IPC channel fd in any way other than <ahref="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a>
or using the IPC channel with a child process that is not a Node.js instance
is not supported.</p>
</li>
<li>
<p><code>'ignore'</code>: Instructs Node.js to ignore the fd in the child. While Node.js
will always open fds 0, 1, and 2 for the processes it spawns, setting the fd
to <code>'ignore'</code> will cause Node.js to open <code>/dev/null</code> and attach it to the
child's fd.</p>
</li>
<li>
<p><code>'inherit'</code>: Pass through the corresponding stdio stream to/from the
parent process. In the first three positions, this is equivalent to
<code>process.stdin</code>, <code>process.stdout</code>, and <code>process.stderr</code>, respectively. In
any other position, equivalent to <code>'ignore'</code>.</p>
</li>
<li>
<p><ahref="stream.html#stream_stream"class="type"><Stream></a> 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 <code>stdio</code> array. The stream must have an
underlying descriptor (file streams do not until the <code>'open'</code> event has
occurred).</p>
</li>
<li>
<p>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 <ahref="stream.html#stream_stream"class="type"><Stream></a> objects can be shared. Passing sockets
is not supported on Windows.</p>
</li>
<li>
<p><code>null</code>, <code>undefined</code>: 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
<p><em>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 <code>unref()</code>) until the
child registers an event handler for the <ahref="process.html#process_event_disconnect"><code>'disconnect'</code></a> event
or the <ahref="process.html#process_event_message"><code>'message'</code></a> event. This allows the child to exit
normally without the process being held open by the open IPC channel.</em></p>
<p>On Unix-like operating systems, the <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> method
performs memory operations synchronously before decoupling the event loop
from the child. Applications with a large memory footprint may find frequent
<ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> calls to be a bottleneck. For more information,
see <ahref="https://bugs.chromium.org/p/v8/issues/detail?id=7381">V8 issue 7381</a>.</p>
<p>See also: <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> and <ahref="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>.</p>
<h2>Synchronous process creation<span><aclass="mark"href="#child_process_synchronous_process_creation"id="child_process_synchronous_process_creation">#</a></span></h2>
<p>The <ahref="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>, <ahref="#child_process_child_process_execsync_command_options"><code>child_process.execSync()</code></a>, and
<ahref="#child_process_child_process_execfilesync_file_args_options"><code>child_process.execFileSync()</code></a> methods are synchronous and will block the
Node.js event loop, pausing execution of any additional code until the spawned
process exits.</p>
<p>Blocking calls like these are mostly useful for simplifying general-purpose
scripting tasks and for simplifying the loading/processing of application
<li><code>file</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The name or path of the executable file to run.</li>
<li><code>args</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string[]></a> List of string arguments.</li>
<li><code>cwd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Current working directory of the child process.</li>
<li><code>input</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"class="type"><TypedArray></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"class="type"><DataView></a> The value which will be passed
as stdin to the spawned process. Supplying this value will override
<code>stdio[0]</code>.</li>
<li><code>stdio</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"class="type"><Array></a> Child's stdio configuration. <code>stderr</code> by default will
be output to the parent process' stderr unless <code>stdio</code> is specified.
<li><code>uid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the user identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li>
<li><code>gid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the group identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li>
<li><code>timeout</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> In milliseconds the maximum amount of time the process
is allowed to run. <strong>Default:</strong><code>undefined</code>.</li>
<li><code>killSignal</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The signal value to be used when the spawned
process will be killed. <strong>Default:</strong><code>'SIGTERM'</code>.</li>
<li><code>maxBuffer</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Largest amount of data in bytes allowed on stdout or
stderr. If exceeded, the child process is terminated. See caveat at
<ahref="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>. <strong>Default:</strong><code>1024 * 1024</code>.</li>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The encoding used for all stdio inputs and outputs.
<li><code>windowsHide</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Hide the subprocess console window that would
normally be created on Windows systems. <strong>Default:</strong><code>false</code>.</li>
<li><code>shell</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
<li>Returns: <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The stdout from the command.</li>
</ul>
<p>The <code>child_process.execFileSync()</code> method is generally identical to
<ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> with the exception that the method will not
return until the child process has fully closed. When a timeout has been
encountered and <code>killSignal</code> is sent, the method won't return until the process
has completely exited.</p>
<p>If the child process intercepts and handles the <code>SIGTERM</code> signal and
does not exit, the parent process will still wait until the child process has
exited.</p>
<p>If the process times out or has a non-zero exit code, this method will throw an
<ahref="errors.html#errors_class_error"><code>Error</code></a> that will include the full result of the underlying
<li><code>command</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The command to run.</li>
<li><code>cwd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Current working directory of the child process.</li>
<li><code>input</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"class="type"><TypedArray></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"class="type"><DataView></a> The value which will be passed
as stdin to the spawned process. Supplying this value will override
<code>stdio[0]</code>.</li>
<li><code>stdio</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"class="type"><Array></a> Child's stdio configuration. <code>stderr</code> by default will
be output to the parent process' stderr unless <code>stdio</code> is specified.
<li><code>shell</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Shell to execute the command with. See
<ahref="#child_process_shell_requirements">Shell requirements</a> and <ahref="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong>
<code>'/bin/sh'</code> on Unix, <code>process.env.ComSpec</code> on Windows.</li>
<li><code>uid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the user identity of the process. (See <ahref="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li>
<li><code>gid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the group identity of the process. (See <ahref="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li>
<li><code>timeout</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> In milliseconds the maximum amount of time the process
is allowed to run. <strong>Default:</strong><code>undefined</code>.</li>
<li><code>killSignal</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The signal value to be used when the spawned
process will be killed. <strong>Default:</strong><code>'SIGTERM'</code>.</li>
<li><code>maxBuffer</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Largest amount of data in bytes allowed on stdout or
stderr. If exceeded, the child process is terminated and any output is
truncated. See caveat at <ahref="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>.
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The encoding used for all stdio inputs and outputs.
<li><code>windowsHide</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Hide the subprocess console window that would
normally be created on Windows systems. <strong>Default:</strong><code>false</code>.</li>
</ul>
</li>
<li>Returns: <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The stdout from the command.</li>
</ul>
<p>The <code>child_process.execSync()</code> method is generally identical to
<ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> with the exception that the method will not return
until the child process has fully closed. When a timeout has been encountered
and <code>killSignal</code> is sent, the method won't return until the process has
completely exited. If the child process intercepts and handles the <code>SIGTERM</code>
signal and doesn't exit, the parent process will wait until the child process
has exited.</p>
<p>If the process times out or has a non-zero exit code, this method will throw.
The <ahref="errors.html#errors_class_error"><code>Error</code></a> object will contain the entire result from
<li><code>command</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The command to run.</li>
<li><code>args</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string[]></a> List of string arguments.</li>
<li><code>cwd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Current working directory of the child process.</li>
<li><code>input</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"class="type"><TypedArray></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"class="type"><DataView></a> The value which will be passed
as stdin to the spawned process. Supplying this value will override
<code>stdio[0]</code>.</li>
<li><code>argv0</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> Explicitly set the value of <code>argv[0]</code> sent to the child
process. This will be set to <code>command</code> if not specified.</li>
<li><code>uid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the user identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li>
<li><code>gid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Sets the group identity of the process (see <ahref="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li>
<li><code>timeout</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> In milliseconds the maximum amount of time the process
is allowed to run. <strong>Default:</strong><code>undefined</code>.</li>
<li><code>killSignal</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The signal value to be used when the spawned
process will be killed. <strong>Default:</strong><code>'SIGTERM'</code>.</li>
<li><code>maxBuffer</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Largest amount of data in bytes allowed on stdout or
stderr. If exceeded, the child process is terminated and any output is
truncated. See caveat at <ahref="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>.
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The encoding used for all stdio inputs and outputs.
<li><code>shell</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
<li><code>windowsVerbatimArguments</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> No quoting or escaping of arguments is
done on Windows. Ignored on Unix. This is set to <code>true</code> automatically
when <code>shell</code> is specified and is CMD. <strong>Default:</strong><code>false</code>.</li>
<li><code>windowsHide</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Hide the subprocess console window that would
normally be created on Windows systems. <strong>Default:</strong><code>false</code>.</li>
<li><code>pid</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Pid of the child process.</li>
<li><code>output</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"class="type"><Array></a> Array of results from stdio output.</li>
<li><code>stdout</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The contents of <code>output[1]</code>.</li>
<li><code>stderr</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The contents of <code>output[2]</code>.</li>
<li><code>status</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type"class="type"><null></a> The exit code of the subprocess, or <code>null</code> if the
subprocess terminated due to a signal.</li>
<li><code>signal</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type"class="type"><null></a> The signal used to kill the subprocess, or <code>null</code> if
the subprocess did not terminate due to a signal.</li>
<li><code>error</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error"class="type"><Error></a> The error object if the child process failed or timed out.</li>
</ul>
</li>
</ul>
<p>The <code>child_process.spawnSync()</code> method is generally identical to
<ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> with the exception that the function will not return
until the child process has fully closed. When a timeout has been encountered
and <code>killSignal</code> is sent, the method won't return until the process has
completely exited. If the process intercepts and handles the <code>SIGTERM</code> signal
and doesn't exit, the parent process will wait until the child process has
exited.</p>
<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this
function. Any input containing shell metacharacters may be used to trigger
<p>Instances of the <code>ChildProcess</code> represent spawned child processes.</p>
<p>Instances of <code>ChildProcess</code> are not intended to be created directly. Rather,
use the <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>, <ahref="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>,
<ahref="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a>, or <ahref="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a> methods to create
<li><code>code</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> The exit code if the child exited on its own.</li>
<li><code>signal</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The signal by which the child process was terminated.</li>
</ul>
<p>The <code>'close'</code> event is emitted when the stdio streams of a child process have
been closed. This is distinct from the <ahref="#child_process_event_exit"><code>'exit'</code></a> event, since multiple
<spanclass="hljs-built_in">console</span>.log(<spanclass="hljs-string">`child process close all stdio with code <spanclass="hljs-subst">${code}</span>`</span>);
<spanclass="hljs-built_in">console</span>.log(<spanclass="hljs-string">`child process exited with code <spanclass="hljs-subst">${code}</span>`</span>);
<li><code>err</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error"class="type"><Error></a> The error.</li>
</ul>
<p>The <code>'error'</code> event is emitted whenever:</p>
<ol>
<li>The process could not be spawned, or</li>
<li>The process could not be killed, or</li>
<li>Sending a message to the child process failed.</li>
</ol>
<p>The <code>'exit'</code> event may or may not fire after an error has occurred. When
listening to both the <code>'exit'</code> and <code>'error'</code> events, guard
against accidentally invoking handler functions multiple times.</p>
<p>See also <ahref="#child_process_subprocess_kill_signal"><code>subprocess.kill()</code></a> and <ahref="#child_process_subprocess_send_message_sendhandle_options_callback"><code>subprocess.send()</code></a>.</p>
<li><code>code</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> The exit code if the child exited on its own.</li>
<li><code>signal</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The signal by which the child process was terminated.</li>
</ul>
<p>The <code>'exit'</code> event is emitted after the child process ends. If the process
exited, <code>code</code> is the final exit code of the process, otherwise <code>null</code>. If the
process terminated due to receipt of a signal, <code>signal</code> is the string name of
the signal, otherwise <code>null</code>. One of the two will always be non-<code>null</code>.</p>
<p>When the <code>'exit'</code> event is triggered, child process stdio streams might still be
open.</p>
<p>Node.js establishes signal handlers for <code>SIGINT</code> and <code>SIGTERM</code> and Node.js
processes will not terminate immediately due to receipt of those signals.
Rather, Node.js will perform a sequence of cleanup actions and then will
<li><code>message</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object"class="type"><Object></a> A parsed JSON object or primitive value.</li>
<li><code>sendHandle</code><ahref="net.html#net_server_listen_handle_backlog_callback"class="type"><Handle></a> A <ahref="net.html#net_class_net_socket"><code>net.Socket</code></a> or <ahref="net.html#net_class_net_server"><code>net.Server</code></a> object, or
undefined.</li>
</ul>
<p>The <code>'message'</code> event is triggered when a child process uses
<ahref="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a> to send messages.</p>
<p>The message goes through serialization and parsing. The resulting
message might not be the same as what is originally sent.</p>
<p>If the <code>serialization</code> option was set to <code>'advanced'</code> used when spawning the
child process, the <code>message</code> argument can contain data that JSON is not able
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object"class="type"><Object></a> A pipe representing the IPC channel to the child process.</li>
</ul>
<p>The <code>subprocess.channel</code> property is a reference to the child's IPC channel. If
no IPC channel currently exists, this property is <code>undefined</code>.</p>
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Set to <code>false</code> after <code>subprocess.disconnect()</code> is called.</li>
</ul>
<p>The <code>subprocess.connected</code> property indicates whether it is still possible to
send and receive messages from a child process. When <code>subprocess.connected</code> is
<code>false</code>, it is no longer possible to send or receive messages.</p>
<p>Closes the IPC channel between parent and child, allowing the child to exit
gracefully once there are no other connections keeping it alive. After calling
this method the <code>subprocess.connected</code> and <code>process.connected</code> properties in
both the parent and child (respectively) will be set to <code>false</code>, and it will be
no longer possible to pass messages between the processes.</p>
<p>The <code>'disconnect'</code> event will be emitted when there are no messages in the
process of being received. This will most often be triggered immediately after
calling <code>subprocess.disconnect()</code>.</p>
<p>When the child process is a Node.js instance (e.g. spawned using
<ahref="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>), the <code>process.disconnect()</code> method can be invoked
within the child process to close the IPC channel as well.</p>
<p>The <code>subprocess.kill()</code> method sends a signal to the child process. If no
argument is given, the process will be sent the <code>'SIGTERM'</code> signal. See
<ahref="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> for a list of available signals. This function returns <code>true</code> if
<ahref="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> succeeds, and <code>false</code> otherwise.</p>
<p>The <ahref="#child_process_child_process"><code>ChildProcess</code></a> object may emit an <ahref="#child_process_event_error"><code>'error'</code></a> event if the signal
cannot be delivered. Sending a signal to a child process that has already exited
is not an error but may have unforeseen consequences. Specifically, if the
process identifier (PID) has been reassigned to another process, the signal will
be delivered to that process instead which can have unexpected results.</p>
<p>While the function is called <code>kill</code>, the signal delivered to the child process
may not actually terminate the process.</p>
<p>See <ahref="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for reference.</p>
<p>On Linux, child processes of child processes will not be terminated
when attempting to kill their parent. This is likely to happen when running a
new process in a shell or with the use of the <code>shell</code> option of <code>ChildProcess</code>:</p>
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> Set to <code>true</code> after <code>subprocess.kill()</code> is used to successfully
send a signal to the child process.</li>
</ul>
<p>The <code>subprocess.killed</code> property indicates whether the child process
successfully received a signal from <code>subprocess.kill()</code>. The <code>killed</code> property
does not indicate that the child process has been terminated.</p>
<li><code>options</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object"class="type"><Object></a> The <code>options</code> argument, if present, is an object used to
<li><code>keepOpen</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a> A value that can be used when passing instances of
<code>net.Socket</code>. When <code>true</code>, the socket is kept open in the sending process.
<p>When an IPC channel has been established between the parent and child (
i.e. when using <ahref="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>), the <code>subprocess.send()</code> method can
be used to send messages to the child process. When the child process is a
Node.js instance, these messages can be received via the <ahref="process.html#process_event_message"><code>'message'</code></a> event.</p>
<p>The message goes through serialization and parsing. The resulting
message might not be the same as what is originally sent.</p>
<p>Child Node.js processes will have a <ahref="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a> method of their own
that allows the child to send messages back to the parent.</p>
<p>There is a special case when sending a <code>{cmd: 'NODE_foo'}</code> message. Messages
containing a <code>NODE_</code> prefix in the <code>cmd</code> property are reserved for use within
Node.js core and will not be emitted in the child's <ahref="process.html#process_event_message"><code>'message'</code></a>
event. Rather, such messages are emitted using the
<code>'internalMessage'</code> event and are consumed internally by Node.js.
Applications should avoid using such messages or listening for
<code>'internalMessage'</code> events as it is subject to change without notice.</p>
<p>The optional <code>sendHandle</code> argument that may be passed to <code>subprocess.send()</code> is
for passing a TCP server or socket object to the child process. The child will
receive the object as the second argument passed to the callback function
registered on the <ahref="process.html#process_event_message"><code>'message'</code></a> event. Any data that is received
and buffered in the socket will not be sent to the child.</p>
<p>The optional <code>callback</code> is a function that is invoked after the message is
sent but before the child may have received it. The function is called with a
single argument: <code>null</code> on success, or an <ahref="errors.html#errors_class_error"><code>Error</code></a> object on failure.</p>
<p>If no <code>callback</code> function is provided and the message cannot be sent, an
<code>'error'</code> event will be emitted by the <ahref="#child_process_child_process"><code>ChildProcess</code></a> object. This can
happen, for instance, when the child process has already exited.</p>
<p><code>subprocess.send()</code> will return <code>false</code> if the channel has closed or when the
backlog of unsent messages exceeds a threshold that makes it unwise to send
more. Otherwise, the method returns <code>true</code>. The <code>callback</code> function can be
used to implement flow control.</p>
<h4>Example: sending a server object<span><aclass="mark"href="#child_process_example_sending_a_server_object"id="child_process_example_sending_a_server_object">#</a></span></h4>
<p>The <code>sendHandle</code> argument can be used, for instance, to pass the handle of
a TCP server object to the child process as illustrated in the example below:</p>
<p>Once the server is now shared between the parent and child, some connections
can be handled by the parent and some by the child.</p>
<p>While the example above uses a server created using the <code>net</code> module, <code>dgram</code>
module servers use exactly the same workflow with the exceptions of listening on
a <code>'message'</code> event instead of <code>'connection'</code> and using <code>server.bind()</code> instead
of <code>server.listen()</code>. This is, however, currently only supported on Unix
platforms.</p>
<h4>Example: sending a socket object<span><aclass="mark"href="#child_process_example_sending_a_socket_object"id="child_process_example_sending_a_socket_object">#</a></span></h4>
<p>Similarly, the <code>sendHandler</code> argument can be used to pass the handle of a
socket to the child process. The example below spawns two children that each
handle connections with "normal" or "special" priority:</p>
<spanclass="hljs-comment">// Check that the client socket exists.</span>
<spanclass="hljs-comment">// It is possible for the socket to be closed between the time it is</span>
<spanclass="hljs-comment">// sent and the time it is received in the child process.</span>
socket.end(<spanclass="hljs-string">`Request handled with <spanclass="hljs-subst">${process.argv[<spanclass="hljs-number">2</span>]}</span> priority`</span>);
<p>A sparse array of pipes to the child process, corresponding with positions in
the <ahref="#child_process_options_stdio"><code>stdio</code></a> option passed to <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> that have been set
to the value <code>'pipe'</code>. <code>subprocess.stdio[0]</code>, <code>subprocess.stdio[1]</code>, and
<code>subprocess.stdio[2]</code> are also available as <code>subprocess.stdin</code>,
<code>subprocess.stdout</code>, and <code>subprocess.stderr</code>, respectively.</p>
<p>In the following example, only the child's fd <code>1</code> (stdout) is configured as a
pipe, so only the parent's <code>subprocess.stdio[1]</code> is a stream, all other values
<spanclass="hljs-number">0</span>, <spanclass="hljs-comment">// Use parent's stdin for child.</span>
<spanclass="hljs-string">'pipe'</span>, <spanclass="hljs-comment">// Pipe child's stdout to parent.</span>
fs.openSync(<spanclass="hljs-string">'err.out'</span>, <spanclass="hljs-string">'w'</span>) <spanclass="hljs-comment">// Direct child's stderr to a file.</span>
<h2><code>maxBuffer</code> and Unicode<span><aclass="mark"href="#child_process_maxbuffer_and_unicode"id="child_process_maxbuffer_and_unicode">#</a></span></h2>
<p>The <code>maxBuffer</code> option specifies the largest number of bytes allowed on <code>stdout</code>
or <code>stderr</code>. If this value is exceeded, then the child process is terminated.
This impacts output that includes multibyte character encodings such as UTF-8 or
UTF-16. For instance, <code>console.log('中文测试')</code> will send 13 UTF-8 encoded bytes
to <code>stdout</code> although there are only 4 characters.</p>
<p>Child processes support a serialization mechanism for IPC that is based on the
<ahref="v8.html#v8_serialization_api">serialization API of the <code>v8</code> module</a>, based on the
<ahref="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>. This is generally more powerful and
supports more built-in JavaScript object types, such as <code>BigInt</code>, <code>Map</code>
and <code>Set</code>, <code>ArrayBuffer</code> and <code>TypedArray</code>, <code>Buffer</code>, <code>Error</code>, <code>RegExp</code> etc.</p>
<p>However, this format is not a full superset of JSON, and e.g. properties set on
objects of such built-in types will not be passed on through the serialization
step. Additionally, performance may not be equivalent to that of JSON, depending
on the structure of the passed data.
Therefore, this feature requires opting in by setting the
<code>serialization</code> option to <code>'advanced'</code> when calling <ahref="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>
or <ahref="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>.</p>