Skip to content

Commit fd27dc7

Browse files
TrottMylesBorins
authored andcommitted
doc: change child to subprocess
Backport-PR-URL: #14635 PR-URL: #14578 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 8c9f1b3 commit fd27dc7

File tree

1 file changed

+76
-65
lines changed

1 file changed

+76
-65
lines changed

doc/api/child_process.md

+76-65
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ Example of checking for failed exec:
400400

401401
```js
402402
const spawn = require('child_process').spawn;
403-
const child = spawn('bad_command');
403+
const subprocess = spawn('bad_command');
404404

405-
child.on('error', (err) => {
406-
console.log('Failed to start child process.');
405+
subprocess.on('error', (err) => {
406+
console.log('Failed to start subprocess.');
407407
});
408408
```
409409

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

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

431431
When using the `detached` option to start a long-running process, the process
432432
will not stay running in the background after the parent exits unless it is
@@ -440,12 +440,12 @@ Example of a long-running process, by detaching and also ignoring its parent
440440
```js
441441
const spawn = require('child_process').spawn;
442442

443-
const child = spawn(process.argv[0], ['child_program.js'], {
443+
const subprocess = spawn(process.argv[0], ['child_program.js'], {
444444
detached: true,
445445
stdio: 'ignore'
446446
});
447447

448-
child.unref();
448+
subprocess.unref();
449449
```
450450

451451
Alternatively one can redirect the child process' output into files:
@@ -456,12 +456,12 @@ const spawn = require('child_process').spawn;
456456
const out = fs.openSync('./out.log', 'a');
457457
const err = fs.openSync('./out.log', 'a');
458458

459-
const child = spawn('prg', [], {
459+
const subprocess = spawn('prg', [], {
460460
detached: true,
461461
stdio: [ 'ignore', out, err ]
462462
});
463463

464-
child.unref();
464+
subprocess.unref();
465465
```
466466

467467
#### options.stdio
@@ -471,9 +471,10 @@ added: v0.7.10
471471

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

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

@@ -763,46 +764,49 @@ added: v0.5.9
763764
The `'message'` event is triggered when a child process uses `process.send()`
764765
to send messages.
765766

766-
### child.connected
767+
<a name="child_process_child_connected"></a>
768+
### subprocess.connected
767769
<!-- YAML
768770
added: v0.7.2
769771
-->
770772

771773
* {Boolean} Set to false after `.disconnect` is called
772774

773-
The `child.connected` property indicates whether it is still possible to send
774-
and receive messages from a child process. When `child.connected` is false, it
775-
is no longer possible to send or receive messages.
775+
The `subprocess.connected` property indicates whether it is still possible to
776+
send and receive messages from a child process. When `subprocess.connected` is
777+
false, it is no longer possible to send or receive messages.
776778

777-
### child.disconnect()
779+
<a name="child_process_child_disconnect"></a>
780+
### subprocess.disconnect()
778781
<!-- YAML
779782
added: v0.7.2
780783
-->
781784

782785
Closes the IPC channel between parent and child, allowing the child to exit
783786
gracefully once there are no other connections keeping it alive. After calling
784-
this method the `child.connected` and `process.connected` properties in both
785-
the parent and child (respectively) will be set to `false`, and it will be no
786-
longer possible to pass messages between the processes.
787+
this method the `subprocess.connected` and `process.connected` properties in
788+
both the parent and child (respectively) will be set to `false`, and it will be
789+
no longer possible to pass messages between the processes.
787790

788791
The `'disconnect'` event will be emitted when there are no messages in the
789792
process of being received. This will most often be triggered immediately after
790-
calling `child.disconnect()`.
793+
calling `subprocess.disconnect()`.
791794

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

796-
### child.kill([signal])
799+
<a name="child_process_child_kill_signal"></a>
800+
### subprocess.kill([signal])
797801
<!-- YAML
798802
added: v0.1.90
799803
-->
800804

801805
* `signal` {String}
802806

803-
The `child.kill()` methods sends a signal to the child process. If no argument
804-
is given, the process will be sent the `'SIGTERM'` signal. See signal(7) for
805-
a list of available signals.
807+
The `subprocess.kill()` methods sends a signal to the child process. If no
808+
argument is given, the process will be sent the `'SIGTERM'` signal. See
809+
signal(7) for a list of available signals.
806810

807811
```js
808812
const spawn = require('child_process').spawn;
@@ -837,7 +841,7 @@ as in this example:
837841
'use strict';
838842
const spawn = require('child_process').spawn;
839843

840-
let child = spawn('sh', ['-c',
844+
let subprocess = spawn('sh', ['-c',
841845
`node -e "setInterval(() => {
842846
console.log(process.pid, 'is alive')
843847
}, 500);"`
@@ -846,11 +850,12 @@ let child = spawn('sh', ['-c',
846850
});
847851

848852
setTimeout(() => {
849-
child.kill(); // does not terminate the node process in the shell
853+
subprocess.kill(); // does not terminate the node process in the shell
850854
}, 2000);
851855
```
852856

853-
### child.pid
857+
<a name="child_process_child_pid"></a>
858+
### subprocess.pid
854859
<!-- YAML
855860
added: v0.1.90
856861
-->
@@ -869,7 +874,8 @@ console.log(`Spawned child pid: ${grep.pid}`);
869874
grep.stdin.end();
870875
```
871876

872-
### child.send(message[, sendHandle][, callback])
877+
<a name="child_process_child_send_message_sendhandle_options_callback"></a>
878+
### subprocess.send(message[, sendHandle[, options]][, callback])
873879
<!-- YAML
874880
added: v0.5.9
875881
-->
@@ -880,9 +886,10 @@ added: v0.5.9
880886
* Returns: {Boolean}
881887

882888
When an IPC channel has been established between the parent and child (
883-
i.e. when using [`child_process.fork()`][]), the `child.send()` method can be
884-
used to send messages to the child process. When the child process is a Node.js
885-
instance, these messages can be received via the `process.on('message')` event.
889+
i.e. when using [`child_process.fork()`][]), the `subprocess.send()` method can
890+
be used to send messages to the child process. When the child process is a
891+
Node.js instance, these messages can be received via the `process.on('message')`
892+
event.
886893

887894
For example, in the parent script:
888895

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

921-
The optional `sendHandle` argument that may be passed to `child.send()` is for
922-
passing a TCP server or socket object to the child process. The child will
928+
The optional `sendHandle` argument that may be passed to `subprocess.send()` is
929+
for passing a TCP server or socket object to the child process. The child will
923930
receive the object as the second argument passed to the callback function
924931
registered on the `process.on('message')` event. Any data that is received and
925932
buffered in the socket will not be sent to the child.
@@ -932,7 +939,7 @@ If no `callback` function is provided and the message cannot be sent, an
932939
`'error'` event will be emitted by the `ChildProcess` object. This can happen,
933940
for instance, when the child process has already exited.
934941

935-
`child.send()` will return `false` if the channel has closed or when the
942+
`subprocess.send()` will return `false` if the channel has closed or when the
936943
backlog of unsent messages exceeds a threshold that makes it unwise to send
937944
more. Otherwise, the method returns `true`. The `callback` function can be
938945
used to implement flow control.
@@ -943,15 +950,15 @@ The `sendHandle` argument can be used, for instance, to pass the handle of
943950
a TCP server object to the child process as illustrated in the example below:
944951

945952
```js
946-
const child = require('child_process').fork('child.js');
953+
const subprocess = require('child_process').fork('subprocess.js');
947954

948955
// Open up the server object and send the handle.
949956
const server = require('net').createServer();
950957
server.on('connection', (socket) => {
951958
socket.end('handled by parent');
952959
});
953960
server.listen(1337, () => {
954-
child.send('server', server);
961+
subprocess.send('server', server);
955962
});
956963
```
957964

@@ -982,8 +989,8 @@ socket to the child process. The example below spawns two children that each
982989
handle connections with "normal" or "special" priority:
983990

984991
```js
985-
const normal = require('child_process').fork('child.js', ['normal']);
986-
const special = require('child_process').fork('child.js', ['special']);
992+
const normal = require('child_process').fork('subprocess.js', ['normal']);
993+
const special = require('child_process').fork('subprocess.js', ['special']);
987994

988995
// Open up the server and send sockets to child
989996
const server = require('net').createServer();
@@ -1000,8 +1007,8 @@ server.on('connection', (socket) => {
10001007
server.listen(1337);
10011008
```
10021009

1003-
The `child.js` would receive the socket handle as the second argument passed
1004-
to the event callback function:
1010+
The `subprocess.js` would receive the socket handle as the second argument
1011+
passed to the event callback function:
10051012

10061013
```js
10071014
process.on('message', (m, socket) => {
@@ -1018,7 +1025,8 @@ this occurs.
10181025

10191026
*Note: this function uses [`JSON.stringify()`][] internally to serialize the `message`.*
10201027

1021-
### child.stderr
1028+
<a name="child_process_child_stderr"></a>
1029+
### subprocess.stderr
10221030
<!-- YAML
10231031
added: v0.1.90
10241032
-->
@@ -1030,10 +1038,11 @@ A `Readable Stream` that represents the child process's `stderr`.
10301038
If the child was spawned with `stdio[2]` set to anything other than `'pipe'`,
10311039
then this will be `undefined`.
10321040

1033-
`child.stderr` is an alias for `child.stdio[2]`. Both properties will refer to
1034-
the same value.
1041+
`subprocess.stderr` is an alias for `subprocess.stdio[2]`. Both properties will
1042+
refer to the same value.
10351043

1036-
### child.stdin
1044+
<a name="child_process_child_stdin"></a>
1045+
### subprocess.stdin
10371046
<!-- YAML
10381047
added: v0.1.90
10391048
-->
@@ -1048,10 +1057,11 @@ continue until this stream has been closed via `end()`.*
10481057
If the child was spawned with `stdio[0]` set to anything other than `'pipe'`,
10491058
then this will be `undefined`.
10501059

1051-
`child.stdin` is an alias for `child.stdio[0]`. Both properties will refer to
1052-
the same value.
1060+
`subprocess.stdin` is an alias for `subprocess.stdio[0]`. Both properties will
1061+
refer to the same value.
10531062

1054-
### child.stdio
1063+
<a name="child_process_child_stdio"></a>
1064+
### subprocess.stdio
10551065
<!-- YAML
10561066
added: v0.7.10
10571067
-->
@@ -1060,38 +1070,39 @@ added: v0.7.10
10601070

10611071
A sparse array of pipes to the child process, corresponding with positions in
10621072
the [`stdio`][] option passed to [`child_process.spawn()`][] that have been set
1063-
to the value `'pipe'`. Note that `child.stdio[0]`, `child.stdio[1]`, and
1064-
`child.stdio[2]` are also available as `child.stdin`, `child.stdout`, and
1065-
`child.stderr`, respectively.
1073+
to the value `'pipe'`. Note that `subprocess.stdio[0]`, `subprocess.stdio[1]`,
1074+
and `subprocess.stdio[2]` are also available as `subprocess.stdin`,
1075+
`subprocess.stdout`, and `subprocess.stderr`, respectively.
10661076

10671077
In the following example, only the child's fd `1` (stdout) is configured as a
1068-
pipe, so only the parent's `child.stdio[1]` is a stream, all other values in
1069-
the array are `null`.
1078+
pipe, so only the parent's `subprocess.stdio[1]` is a stream, all other values
1079+
in the array are `null`.
10701080

10711081
```js
10721082
const assert = require('assert');
10731083
const fs = require('fs');
10741084
const child_process = require('child_process');
10751085

1076-
const child = child_process.spawn('ls', {
1086+
const subprocess = child_process.spawn('ls', {
10771087
stdio: [
10781088
0, // Use parents stdin for child
10791089
'pipe', // Pipe child's stdout to parent
10801090
fs.openSync('err.out', 'w') // Direct child's stderr to a file
10811091
]
10821092
});
10831093

1084-
assert.equal(child.stdio[0], null);
1085-
assert.equal(child.stdio[0], child.stdin);
1094+
assert.equal(subprocess.stdio[0], null);
1095+
assert.equal(subprocess.stdio[0], subprocess.stdin);
10861096

1087-
assert(child.stdout);
1088-
assert.equal(child.stdio[1], child.stdout);
1097+
assert(subprocess.stdout);
1098+
assert.equal(subprocess.stdio[1], subprocess.stdout);
10891099

1090-
assert.equal(child.stdio[2], null);
1091-
assert.equal(child.stdio[2], child.stderr);
1100+
assert.equal(subprocess.stdio[2], null);
1101+
assert.equal(subprocess.stdio[2], subprocess.stderr);
10921102
```
10931103

1094-
### child.stdout
1104+
<a name="child_process_child_stdout"></a>
1105+
### subprocess.stdout
10951106
<!-- YAML
10961107
added: v0.1.90
10971108
-->
@@ -1103,8 +1114,8 @@ A `Readable Stream` that represents the child process's `stdout`.
11031114
If the child was spawned with `stdio[1]` set to anything other than `'pipe'`,
11041115
then this will be `undefined`.
11051116

1106-
`child.stdout` is an alias for `child.stdio[1]`. Both properties will refer
1107-
to the same value.
1117+
`subprocess.stdout` is an alias for `subprocess.stdio[1]`. Both properties will
1118+
refer to the same value.
11081119

11091120
[`popen(3)`]: http://linux.die.net/man/3/popen
11101121
[`ChildProcess`]: #child_process_child_process

0 commit comments

Comments
 (0)