Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 3a08b7c

Browse files
sam-githubchrisdickinson
authored andcommitted
doc: cover stdio option in child_process
- Add hyperlinks from spawn options to subsections detailing what those options do. - Clarify some verbiage around ChildProcess.prototype.std{in,out,err}. - Remove second-person pronoun. PR-URL: #8639 Reviewed-by: Chris Dickinson <[email protected]>
1 parent a1b2875 commit 3a08b7c

1 file changed

Lines changed: 68 additions & 33 deletions

File tree

doc/api/child_process.markdown

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,71 @@ Messages send by `.send(message, [sendHandle])` are obtained using the
9090
* {Stream object}
9191

9292
A `Writable Stream` that represents the child process's `stdin`.
93-
Closing this stream via `end()` often causes the child process to terminate.
93+
If the child is waiting to read all its input, it will not continue until this
94+
stream has been closed via `end()`.
9495

95-
If the child stdio streams are shared with the parent, then this will
96+
If the child was not spawned with `stdio[0]` set to `'pipe'`, then this will
9697
not be set.
9798

99+
`child.stdin` is shorthand for `child.stdio[0]`. Both properties will refer
100+
to the same object, or null.
101+
98102
### child.stdout
99103

100104
* {Stream object}
101105

102106
A `Readable Stream` that represents the child process's `stdout`.
103107

104-
If the child stdio streams are shared with the parent, then this will
108+
If the child was not spawned with `stdio[1]` set to `'pipe'`, then this will
105109
not be set.
106110

111+
`child.stdout` is shorthand for `child.stdio[1]`. Both properties will refer
112+
to the same object, or null.
113+
107114
### child.stderr
108115

109116
* {Stream object}
110117

111118
A `Readable Stream` that represents the child process's `stderr`.
112119

113-
If the child stdio streams are shared with the parent, then this will
120+
If the child was not spawned with `stdio[2]` set to `'pipe'`, then this will
114121
not be set.
115122

123+
`child.stderr` is shorthand for `child.stdio[2]`. Both properties will refer
124+
to the same object, or null.
125+
126+
### child.stdio
127+
128+
* {Array}
129+
130+
A sparse array of pipes to the child process, corresponding with positions in
131+
the [stdio](#child_process_options_stdio) option to
132+
[spawn](#child_process_child_process_spawn_command_args_options) that have been
133+
set to `'pipe'`.
134+
Note that streams 0-2 are also available as ChildProcess.stdin,
135+
ChildProcess.stdout, and ChildProcess.stderr, respectively.
136+
137+
In the following example, only the child's fd `1` is setup as a pipe, so only
138+
the parent's `child.stdio[1]` is a stream, all other values in the array are
139+
`null`.
140+
141+
child = child_process.spawn("ls", {
142+
stdio: [
143+
0, // use parents stdin for child
144+
'pipe', // pipe child's stdout to parent
145+
fs.openSync("err.out", "w") // direct child's stderr to a file
146+
]
147+
});
148+
149+
assert.equal(child.stdio[0], null);
150+
assert.equal(child.stdio[0], child.stdin);
151+
152+
assert(child.stdout);
153+
assert.equal(child.stdio[1], child.stdout);
154+
155+
assert.equal(child.stdio[2], null);
156+
assert.equal(child.stdio[2], child.stderr);
157+
116158
### child.pid
117159

118160
* {Integer}
@@ -299,11 +341,13 @@ child process has any open IPC channels with the parent (i.e `fork()`).
299341
* `args` {Array} List of string arguments
300342
* `options` {Object}
301343
* `cwd` {String} Current working directory of the child process
302-
* `stdio` {Array|String} Child's stdio configuration. (See below)
303-
* `customFds` {Array} **Deprecated** File descriptors for the child to use
304-
for stdio. (See below)
305344
* `env` {Object} Environment key-value pairs
306-
* `detached` {Boolean} The child will be a process group leader. (See below)
345+
* `stdio` {Array|String} Child's stdio configuration. (See
346+
[below](#child_process_options_stdio))
347+
* `customFds` {Array} **Deprecated** File descriptors for the child to use
348+
for stdio. (See [below](#child_process_options_customFds))
349+
* `detached` {Boolean} The child will be a process group leader. (See
350+
[below](#child_process_options_detached))
307351
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
308352
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
309353
* return: {ChildProcess object}
@@ -317,8 +361,11 @@ The third argument is used to specify additional options, which defaults to:
317361
env: process.env
318362
}
319363

320-
`cwd` allows you to specify the working directory from which the process is spawned.
321-
Use `env` to specify environment variables that will be visible to the new process.
364+
Use `cwd` to specify the working directory from which the process is spawned.
365+
If not given, the default is to inherit the current working directory.
366+
367+
Use `env` to specify environment variables that will be visible to the new
368+
process, the default is `process.env`.
322369

323370
Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the exit code:
324371

@@ -374,24 +421,16 @@ Example: A very elaborate way to run 'ps ax | grep ssh'
374421
});
375422

376423

377-
Example of checking for failed exec:
378-
379-
var spawn = require('child_process').spawn,
380-
child = spawn('bad_command');
424+
### options.stdio
381425

382-
child.stderr.setEncoding('utf8');
383-
child.stderr.on('data', function (data) {
384-
if (/^execvp\(\)/.test(data)) {
385-
console.log('Failed to start child process.');
386-
}
387-
});
426+
As a shorthand, the `stdio` argument may also be one of the following
427+
strings:
388428

389-
Note that if spawn receives an empty options object, it will result in
390-
spawning the process with an empty environment rather than using
391-
`process.env`. This due to backwards compatibility issues with a deprecated
392-
API.
429+
* `'pipe'` - `['pipe', 'pipe', 'pipe']`, this is the default value
430+
* `'ignore'` - `['ignore', 'ignore', 'ignore']`
431+
* `'inherit'` - `[process.stdin, process.stdout, process.stderr]` or `[0,1,2]`
393432

394-
The 'stdio' option to `child_process.spawn()` is an array where each
433+
Otherwise, the 'stdio' option to `child_process.spawn()` is an array where each
395434
index corresponds to a fd in the child. The value is one of the following:
396435

397436
1. `'pipe'` - Create a pipe between the child process and the parent process.
@@ -422,13 +461,6 @@ index corresponds to a fd in the child. The value is one of the following:
422461
words, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the
423462
default is `'ignore'`.
424463

425-
As a shorthand, the `stdio` argument may also be one of the following
426-
strings, rather than an array:
427-
428-
* `ignore` - `['ignore', 'ignore', 'ignore']`
429-
* `pipe` - `['pipe', 'pipe', 'pipe']`
430-
* `inherit` - `[process.stdin, process.stdout, process.stderr]` or `[0,1,2]`
431-
432464
Example:
433465

434466
var spawn = require('child_process').spawn;
@@ -443,6 +475,8 @@ Example:
443475
// startd-style interface.
444476
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
445477

478+
### options.detached
479+
446480
If the `detached` option is set, the child process will be made the leader of a
447481
new process group. This makes it possible for the child to continue running
448482
after the parent exits.
@@ -471,6 +505,8 @@ will not stay running in the background unless it is provided with a `stdio`
471505
configuration that is not connected to the parent. If the parent's `stdio` is
472506
inherited, the child will remain attached to the controlling terminal.
473507

508+
### options.customFds
509+
474510
There is a deprecated option called `customFds` which allows one to specify
475511
specific file descriptors for the stdio of the child process. This API was
476512
not portable to all platforms and therefore removed.
@@ -561,7 +597,6 @@ leaner than `child_process.exec`. It has the same options.
561597
* `options` {Object}
562598
* `cwd` {String} Current working directory of the child process
563599
* `env` {Object} Environment key-value pairs
564-
* `encoding` {String} (Default: 'utf8')
565600
* `execPath` {String} Executable used to create the child process
566601
* `execArgv` {Array} List of string arguments passed to the executable
567602
(Default: `process.execArgv`)

0 commit comments

Comments
 (0)