Skip to content

Commit 73c720d

Browse files
TrottMylesBorins
authored andcommittedAug 12, 2017
doc: change child to subprocess
Backport-PR-URL: #14633 PR-URL: #14578 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 67ce52c commit 73c720d

File tree

1 file changed

+100
-87
lines changed

1 file changed

+100
-87
lines changed
 

‎doc/api/child_process.md

+100-87
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ The `child_process.fork()` method is a special case of
280280
Like [`child_process.spawn()`][], a [`ChildProcess`][] object is returned. The returned
281281
[`ChildProcess`][] will have an additional communication channel built-in that
282282
allows messages to be passed back and forth between the parent and child. See
283-
[`child.send()`][] for details.
283+
[`subprocess.send()`][] for details.
284284

285285
It is important to keep in mind that spawned Node.js child processes are
286286
independent of the parent with exception of the IPC communication channel
@@ -412,10 +412,10 @@ Example of checking for failed exec:
412412

413413
```js
414414
const spawn = require('child_process').spawn;
415-
const child = spawn('bad_command');
415+
const subprocess = spawn('bad_command');
416416

417-
child.on('error', (err) => {
418-
console.log('Failed to start child process.');
417+
subprocess.on('error', (err) => {
418+
console.log('Failed to start subprocess.');
419419
});
420420
```
421421

@@ -443,10 +443,10 @@ child processes may continue running after the parent exits regardless of
443443
whether they are detached or not. See setsid(2) for more information.
444444

445445
By default, the parent will wait for the detached child to exit. To prevent
446-
the parent from waiting for a given `child`, use the `child.unref()` method.
447-
Doing so will cause the parent's event loop to not include the child in its
448-
reference count, allowing the parent to exit independently of the child, unless
449-
there is an established IPC channel between the child and parent.
446+
the parent from waiting for a given `subprocess`, use the `subprocess.unref()`
447+
method. Doing so will cause the parent's event loop to not include the child in
448+
its reference count, allowing the parent to exit independently of the child,
449+
unless there is an established IPC channel between the child and parent.
450450

451451
When using the `detached` option to start a long-running process, the process
452452
will not stay running in the background after the parent exits unless it is
@@ -460,12 +460,12 @@ Example of a long-running process, by detaching and also ignoring its parent
460460
```js
461461
const spawn = require('child_process').spawn;
462462

463-
const child = spawn(process.argv[0], ['child_program.js'], {
463+
const subprocess = spawn(process.argv[0], ['child_program.js'], {
464464
detached: true,
465465
stdio: 'ignore'
466466
});
467467

468-
child.unref();
468+
subprocess.unref();
469469
```
470470

471471
Alternatively one can redirect the child process' output into files:
@@ -476,12 +476,12 @@ const spawn = require('child_process').spawn;
476476
const out = fs.openSync('./out.log', 'a');
477477
const err = fs.openSync('./out.log', 'a');
478478

479-
const child = spawn('prg', [], {
479+
const subprocess = spawn('prg', [], {
480480
detached: true,
481481
stdio: [ 'ignore', out, err ]
482482
});
483483

484-
child.unref();
484+
subprocess.unref();
485485
```
486486

487487
#### options.stdio
@@ -491,9 +491,10 @@ added: v0.7.10
491491

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

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

@@ -509,17 +510,18 @@ pipes between the parent and child. The value is one of the following:
509510

510511
1. `'pipe'` - Create a pipe between the child process and the parent process.
511512
The parent end of the pipe is exposed to the parent as a property on the
512-
`child_process` object as [`child.stdio[fd]`][`stdio`]. Pipes created for
513-
fds 0 - 2 are also available as [`child.stdin`][], [`child.stdout`][]
514-
and [`child.stderr`][], respectively.
513+
`child_process` object as [`subprocess.stdio[fd]`][`stdio`]. Pipes created
514+
for fds 0 - 2 are also available as [`subprocess.stdin`][],
515+
[`subprocess.stdout`][] and [`subprocess.stderr`][], respectively.
515516
2. `'ipc'` - Create an IPC channel for passing messages/file descriptors
516517
between parent and child. A [`ChildProcess`][] may have at most *one* IPC stdio
517-
file descriptor. Setting this option enables the [`child.send()`][] method.
518-
If the child writes JSON messages to this file descriptor, the
519-
[`child.on('message')`][`'message'`] event handler will be triggered in the parent.
520-
If the child is a Node.js process, the presence of an IPC channel will enable
521-
[`process.send()`][], [`process.disconnect()`][], [`process.on('disconnect')`][], and
522-
[`process.on('message')`] within the child.
518+
file descriptor. Setting this option enables the [`subprocess.send()`][]
519+
method. If the child writes JSON messages to this file descriptor, the
520+
[`subprocess.on('message')`][`'message'`] event handler will be triggered in
521+
the parent. If the child is a Node.js process, the presence of an IPC channel
522+
will enable [`process.send()`][], [`process.disconnect()`][],
523+
[`process.on('disconnect')`][], and [`process.on('message')`] within the
524+
child.
523525
3. `'ignore'` - Instructs Node.js to ignore the fd in the child. While Node.js
524526
will always open fds 0 - 2 for the processes it spawns, setting the fd to
525527
`'ignore'` will cause Node.js to open `/dev/null` and attach it to the
@@ -739,9 +741,10 @@ added: v0.7.2
739741
-->
740742

741743
The `'disconnect'` event is emitted after calling the
742-
[`child.disconnect()`][] method in parent process or [`process.disconnect()`][] in child process. After
743-
disconnecting it is no longer possible to send or receive messages, and the
744-
[`child.connected`][] property is `false`.
744+
[`subprocess.disconnect()`][] method in parent process or
745+
[`process.disconnect()`][] in child process. After disconnecting it is no longer
746+
possible to send or receive messages, and the [`subprocess.connected`][]
747+
property is `false`.
745748

746749
### Event: 'error'
747750

@@ -757,7 +760,7 @@ Note that the `'exit'` event may or may not fire after an error has occurred.
757760
If you are listening to both the `'exit'` and `'error'` events, it is important
758761
to guard against accidentally invoking handler functions multiple times.
759762

760-
See also [`child.kill()`][] and [`child.send()`][].
763+
See also [`subprocess.kill()`][] and [`subprocess.send()`][].
761764

762765
### Event: 'exit'
763766
<!-- YAML
@@ -794,46 +797,49 @@ added: v0.5.9
794797
The `'message'` event is triggered when a child process uses [`process.send()`][]
795798
to send messages.
796799

797-
### child.connected
800+
<a name="child_process_child_connected"></a>
801+
### subprocess.connected
798802
<!-- YAML
799803
added: v0.7.2
800804
-->
801805

802-
* {boolean} Set to `false` after `child.disconnect()` is called
806+
* {boolean} Set to `false` after `subprocess.disconnect()` is called
803807

804-
The `child.connected` property indicates whether it is still possible to send
805-
and receive messages from a child process. When `child.connected` is `false`, it
806-
is no longer possible to send or receive messages.
808+
The `subprocess.connected` property indicates whether it is still possible to
809+
send and receive messages from a child process. When `subprocess.connected` is
810+
`false`, it is no longer possible to send or receive messages.
807811

808-
### child.disconnect()
812+
<a name="child_process_child_disconnect"></a>
813+
### subprocess.disconnect()
809814
<!-- YAML
810815
added: v0.7.2
811816
-->
812817

813818
Closes the IPC channel between parent and child, allowing the child to exit
814819
gracefully once there are no other connections keeping it alive. After calling
815-
this method the `child.connected` and `process.connected` properties in both
816-
the parent and child (respectively) will be set to `false`, and it will be no
817-
longer possible to pass messages between the processes.
820+
this method the `subprocess.connected` and `process.connected` properties in
821+
both the parent and child (respectively) will be set to `false`, and it will be
822+
no longer possible to pass messages between the processes.
818823

819824
The `'disconnect'` event will be emitted when there are no messages in the
820825
process of being received. This will most often be triggered immediately after
821-
calling `child.disconnect()`.
826+
calling `subprocess.disconnect()`.
822827

823828
Note that when the child process is a Node.js instance (e.g. spawned using
824829
[`child_process.fork()`]), the `process.disconnect()` method can be invoked
825830
within the child process to close the IPC channel as well.
826831

827-
### child.kill([signal])
832+
<a name="child_process_child_kill_signal"></a>
833+
### subprocess.kill([signal])
828834
<!-- YAML
829835
added: v0.1.90
830836
-->
831837

832838
* `signal` {string}
833839

834-
The `child.kill()` methods sends a signal to the child process. If no argument
835-
is given, the process will be sent the `'SIGTERM'` signal. See signal(7) for
836-
a list of available signals.
840+
The `subprocess.kill()` methods sends a signal to the child process. If no
841+
argument is given, the process will be sent the `'SIGTERM'` signal. See
842+
signal(7) for a list of available signals.
837843

838844
```js
839845
const spawn = require('child_process').spawn;
@@ -868,7 +874,7 @@ as in this example:
868874
'use strict';
869875
const spawn = require('child_process').spawn;
870876

871-
const child = spawn(
877+
const subprocess = spawn(
872878
'sh',
873879
[
874880
'-c',
@@ -881,11 +887,12 @@ const child = spawn(
881887
);
882888

883889
setTimeout(() => {
884-
child.kill(); // does not terminate the node process in the shell
890+
subprocess.kill(); // does not terminate the node process in the shell
885891
}, 2000);
886892
```
887893

888-
### child.pid
894+
<a name="child_process_child_pid"></a>
895+
### subprocess.pid
889896
<!-- YAML
890897
added: v0.1.90
891898
-->
@@ -904,7 +911,8 @@ console.log(`Spawned child pid: ${grep.pid}`);
904911
grep.stdin.end();
905912
```
906913

907-
### child.send(message[, sendHandle[, options]][, callback])
914+
<a name="child_process_child_send_message_sendhandle_options_callback"></a>
915+
### subprocess.send(message[, sendHandle[, options]][, callback])
908916
<!-- YAML
909917
added: v0.5.9
910918
-->
@@ -916,9 +924,10 @@ added: v0.5.9
916924
* Returns: {boolean}
917925

918926
When an IPC channel has been established between the parent and child (
919-
i.e. when using [`child_process.fork()`][]), the `child.send()` method can be
920-
used to send messages to the child process. When the child process is a Node.js
921-
instance, these messages can be received via the [`process.on('message')`][] event.
927+
i.e. when using [`child_process.fork()`][]), the `subprocess.send()` method can
928+
be used to send messages to the child process. When the child process is a
929+
Node.js instance, these messages can be received via the
930+
[`process.on('message')`][] event.
922931

923932
For example, in the parent script:
924933

@@ -954,8 +963,8 @@ for use within Node.js core and will not be emitted in the child's
954963
Applications should avoid using such messages or listening for
955964
`'internalMessage'` events as it is subject to change without notice.
956965

957-
The optional `sendHandle` argument that may be passed to `child.send()` is for
958-
passing a TCP server or socket object to the child process. The child will
966+
The optional `sendHandle` argument that may be passed to `subprocess.send()` is
967+
for passing a TCP server or socket object to the child process. The child will
959968
receive the object as the second argument passed to the callback function
960969
registered on the [`process.on('message')`][] event. Any data that is received
961970
and buffered in the socket will not be sent to the child.
@@ -976,7 +985,7 @@ If no `callback` function is provided and the message cannot be sent, an
976985
`'error'` event will be emitted by the [`ChildProcess`][] object. This can happen,
977986
for instance, when the child process has already exited.
978987

979-
`child.send()` will return `false` if the channel has closed or when the
988+
`subprocess.send()` will return `false` if the channel has closed or when the
980989
backlog of unsent messages exceeds a threshold that makes it unwise to send
981990
more. Otherwise, the method returns `true`. The `callback` function can be
982991
used to implement flow control.
@@ -987,15 +996,15 @@ The `sendHandle` argument can be used, for instance, to pass the handle of
987996
a TCP server object to the child process as illustrated in the example below:
988997

989998
```js
990-
const child = require('child_process').fork('child.js');
999+
const subprocess = require('child_process').fork('subprocess.js');
9911000

9921001
// Open up the server object and send the handle.
9931002
const server = require('net').createServer();
9941003
server.on('connection', (socket) => {
9951004
socket.end('handled by parent');
9961005
});
9971006
server.listen(1337, () => {
998-
child.send('server', server);
1007+
subprocess.send('server', server);
9991008
});
10001009
```
10011010

@@ -1026,8 +1035,8 @@ socket to the child process. The example below spawns two children that each
10261035
handle connections with "normal" or "special" priority:
10271036

10281037
```js
1029-
const normal = require('child_process').fork('child.js', ['normal']);
1030-
const special = require('child_process').fork('child.js', ['special']);
1038+
const normal = require('child_process').fork('subprocess.js', ['normal']);
1039+
const special = require('child_process').fork('subprocess.js', ['special']);
10311040

10321041
// Open up the server and send sockets to child
10331042
const server = require('net').createServer();
@@ -1044,8 +1053,8 @@ server.on('connection', (socket) => {
10441053
server.listen(1337);
10451054
```
10461055

1047-
The `child.js` would receive the socket handle as the second argument passed
1048-
to the event callback function:
1056+
The `subprocess.js` would receive the socket handle as the second argument
1057+
passed to the event callback function:
10491058

10501059
```js
10511060
process.on('message', (m, socket) => {
@@ -1063,7 +1072,8 @@ this occurs.
10631072
*Note: this function uses [`JSON.stringify()`][] internally to serialize the
10641073
`message`.*
10651074

1066-
### child.stderr
1075+
<a name="child_process_child_stderr"></a>
1076+
### subprocess.stderr
10671077
<!-- YAML
10681078
added: v0.1.90
10691079
-->
@@ -1075,10 +1085,11 @@ A `Readable Stream` that represents the child process's `stderr`.
10751085
If the child was spawned with `stdio[2]` set to anything other than `'pipe'`,
10761086
then this will be `null`.
10771087

1078-
`child.stderr` is an alias for `child.stdio[2]`. Both properties will refer to
1079-
the same value.
1088+
`subprocess.stderr` is an alias for `subprocess.stdio[2]`. Both properties will
1089+
refer to the same value.
10801090

1081-
### child.stdin
1091+
<a name="child_process_child_stdin"></a>
1092+
### subprocess.stdin
10821093
<!-- YAML
10831094
added: v0.1.90
10841095
-->
@@ -1093,10 +1104,11 @@ continue until this stream has been closed via `end()`.*
10931104
If the child was spawned with `stdio[0]` set to anything other than `'pipe'`,
10941105
then this will be `null`.
10951106

1096-
`child.stdin` is an alias for `child.stdio[0]`. Both properties will refer to
1097-
the same value.
1107+
`subprocess.stdin` is an alias for `subprocess.stdio[0]`. Both properties will
1108+
refer to the same value.
10981109

1099-
### child.stdio
1110+
<a name="child_process_child_stdio"></a>
1111+
### subprocess.stdio
11001112
<!-- YAML
11011113
added: v0.7.10
11021114
-->
@@ -1105,38 +1117,39 @@ added: v0.7.10
11051117

11061118
A sparse array of pipes to the child process, corresponding with positions in
11071119
the [`stdio`][] option passed to [`child_process.spawn()`][] that have been set
1108-
to the value `'pipe'`. Note that `child.stdio[0]`, `child.stdio[1]`, and
1109-
`child.stdio[2]` are also available as `child.stdin`, `child.stdout`, and
1110-
`child.stderr`, respectively.
1120+
to the value `'pipe'`. Note that `subprocess.stdio[0]`, `subprocess.stdio[1]`,
1121+
and `subprocess.stdio[2]` are also available as `subprocess.stdin`,
1122+
`subprocess.stdout`, and `subprocess.stderr`, respectively.
11111123

11121124
In the following example, only the child's fd `1` (stdout) is configured as a
1113-
pipe, so only the parent's `child.stdio[1]` is a stream, all other values in
1114-
the array are `null`.
1125+
pipe, so only the parent's `subprocess.stdio[1]` is a stream, all other values
1126+
in the array are `null`.
11151127

11161128
```js
11171129
const assert = require('assert');
11181130
const fs = require('fs');
11191131
const child_process = require('child_process');
11201132

1121-
const child = child_process.spawn('ls', {
1133+
const subprocess = child_process.spawn('ls', {
11221134
stdio: [
11231135
0, // Use parent's stdin for child
11241136
'pipe', // Pipe child's stdout to parent
11251137
fs.openSync('err.out', 'w') // Direct child's stderr to a file
11261138
]
11271139
});
11281140

1129-
assert.strictEqual(child.stdio[0], null);
1130-
assert.strictEqual(child.stdio[0], child.stdin);
1141+
assert.strictEqual(subprocess.stdio[0], null);
1142+
assert.strictEqual(subprocess.stdio[0], subprocess.stdin);
11311143

1132-
assert(child.stdout);
1133-
assert.strictEqual(child.stdio[1], child.stdout);
1144+
assert(subprocess.stdout);
1145+
assert.strictEqual(subprocess.stdio[1], subprocess.stdout);
11341146

1135-
assert.strictEqual(child.stdio[2], null);
1136-
assert.strictEqual(child.stdio[2], child.stderr);
1147+
assert.strictEqual(subprocess.stdio[2], null);
1148+
assert.strictEqual(subprocess.stdio[2], subprocess.stderr);
11371149
```
11381150

1139-
### child.stdout
1151+
<a name="child_process_child_stdout"></a>
1152+
### subprocess.stdout
11401153
<!-- YAML
11411154
added: v0.1.90
11421155
-->
@@ -1148,8 +1161,8 @@ A `Readable Stream` that represents the child process's `stdout`.
11481161
If the child was spawned with `stdio[1]` set to anything other than `'pipe'`,
11491162
then this will be `null`.
11501163

1151-
`child.stdout` is an alias for `child.stdio[1]`. Both properties will refer
1152-
to the same value.
1164+
`subprocess.stdout` is an alias for `subprocess.stdio[1]`. Both properties will
1165+
refer to the same value.
11531166

11541167
## `maxBuffer` and Unicode
11551168

@@ -1162,13 +1175,13 @@ to `stdout` although there are only 4 characters.
11621175
[`'error'`]: #child_process_event_error
11631176
[`'exit'`]: #child_process_event_exit
11641177
[`'message'`]: #child_process_event_message
1165-
[`child.connected`]: #child_process_child_connected
1166-
[`child.disconnect()`]: #child_process_child_disconnect
1167-
[`child.kill()`]: #child_process_child_kill_signal
1168-
[`child.send()`]: #child_process_child_send_message_sendhandle_options_callback
1169-
[`child.stderr`]: #child_process_child_stderr
1170-
[`child.stdin`]: #child_process_child_stdin
1171-
[`child.stdout`]: #child_process_child_stdout
1178+
[`subprocess.connected`]: #child_process_subprocess_connected
1179+
[`subprocess.disconnect()`]: #child_process_subprocess_disconnect
1180+
[`subprocess.kill()`]: #child_process_subprocess_kill_signal
1181+
[`subprocess.send()`]: #child_process_subprocess_send_message_sendhandle_options_callback
1182+
[`subprocess.stderr`]: #child_process_subprocess_stderr
1183+
[`subprocess.stdin`]: #child_process_subprocess_stdin
1184+
[`subprocess.stdout`]: #child_process_subprocess_stdout
11721185
[`child_process.exec()`]: #child_process_child_process_exec_command_options_callback
11731186
[`child_process.execFile()`]: #child_process_child_process_execfile_file_args_options_callback
11741187
[`child_process.execFileSync()`]: #child_process_child_process_execfilesync_file_args_options

0 commit comments

Comments
 (0)
Please sign in to comment.