@@ -280,7 +280,7 @@ The `child_process.fork()` method is a special case of
280
280
Like [ ` child_process.spawn() ` ] [ ] , a [ ` ChildProcess ` ] [ ] object is returned. The returned
281
281
[ ` ChildProcess ` ] [ ] will have an additional communication channel built-in that
282
282
allows messages to be passed back and forth between the parent and child. See
283
- [ ` child .send()` ] [ ] for details.
283
+ [ ` subprocess .send()` ] [ ] for details.
284
284
285
285
It is important to keep in mind that spawned Node.js child processes are
286
286
independent of the parent with exception of the IPC communication channel
@@ -412,10 +412,10 @@ Example of checking for failed exec:
412
412
413
413
``` js
414
414
const spawn = require (' child_process' ).spawn ;
415
- const child = spawn (' bad_command' );
415
+ const subprocess = spawn (' bad_command' );
416
416
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 .' );
419
419
});
420
420
```
421
421
@@ -443,10 +443,10 @@ child processes may continue running after the parent exits regardless of
443
443
whether they are detached or not. See setsid(2) for more information.
444
444
445
445
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.
450
450
451
451
When using the ` detached ` option to start a long-running process, the process
452
452
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
460
460
``` js
461
461
const spawn = require (' child_process' ).spawn ;
462
462
463
- const child = spawn (process .argv [0 ], [' child_program.js' ], {
463
+ const subprocess = spawn (process .argv [0 ], [' child_program.js' ], {
464
464
detached: true ,
465
465
stdio: ' ignore'
466
466
});
467
467
468
- child .unref ();
468
+ subprocess .unref ();
469
469
```
470
470
471
471
Alternatively one can redirect the child process' output into files:
@@ -476,12 +476,12 @@ const spawn = require('child_process').spawn;
476
476
const out = fs .openSync (' ./out.log' , ' a' );
477
477
const err = fs .openSync (' ./out.log' , ' a' );
478
478
479
- const child = spawn (' prg' , [], {
479
+ const subprocess = spawn (' prg' , [], {
480
480
detached: true ,
481
481
stdio: [ ' ignore' , out, err ]
482
482
});
483
483
484
- child .unref ();
484
+ subprocess .unref ();
485
485
```
486
486
487
487
#### options.stdio
@@ -491,9 +491,10 @@ added: v0.7.10
491
491
492
492
The ` options.stdio ` option is used to configure the pipes that are established
493
493
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'] ` .
497
498
498
499
For convenience, ` options.stdio ` may be one of the following strings:
499
500
@@ -509,17 +510,18 @@ pipes between the parent and child. The value is one of the following:
509
510
510
511
1 . ` 'pipe' ` - Create a pipe between the child process and the parent process.
511
512
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.
515
516
2 . ` 'ipc' ` - Create an IPC channel for passing messages/file descriptors
516
517
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.
523
525
3 . ` 'ignore' ` - Instructs Node.js to ignore the fd in the child. While Node.js
524
526
will always open fds 0 - 2 for the processes it spawns, setting the fd to
525
527
` 'ignore' ` will cause Node.js to open ` /dev/null ` and attach it to the
@@ -739,9 +741,10 @@ added: v0.7.2
739
741
-->
740
742
741
743
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 ` .
745
748
746
749
### Event: 'error'
747
750
@@ -757,7 +760,7 @@ Note that the `'exit'` event may or may not fire after an error has occurred.
757
760
If you are listening to both the ` 'exit' ` and ` 'error' ` events, it is important
758
761
to guard against accidentally invoking handler functions multiple times.
759
762
760
- See also [ ` child .kill()` ] [ ] and [ ` child .send()` ] [ ] .
763
+ See also [ ` subprocess .kill()` ] [ ] and [ ` subprocess .send()` ] [ ] .
761
764
762
765
### Event: 'exit'
763
766
<!-- YAML
@@ -794,46 +797,49 @@ added: v0.5.9
794
797
The ` 'message' ` event is triggered when a child process uses [ ` process.send() ` ] [ ]
795
798
to send messages.
796
799
797
- ### child.connected
800
+ <a name =" child_process_child_connected " ></a >
801
+ ### subprocess.connected
798
802
<!-- YAML
799
803
added: v0.7.2
800
804
-->
801
805
802
- * {boolean} Set to ` false ` after ` child .disconnect()` is called
806
+ * {boolean} Set to ` false ` after ` subprocess .disconnect()` is called
803
807
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.
807
811
808
- ### child.disconnect()
812
+ <a name =" child_process_child_disconnect " ></a >
813
+ ### subprocess.disconnect()
809
814
<!-- YAML
810
815
added: v0.7.2
811
816
-->
812
817
813
818
Closes the IPC channel between parent and child, allowing the child to exit
814
819
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.
818
823
819
824
The ` 'disconnect' ` event will be emitted when there are no messages in the
820
825
process of being received. This will most often be triggered immediately after
821
- calling ` child .disconnect()` .
826
+ calling ` subprocess .disconnect()` .
822
827
823
828
Note that when the child process is a Node.js instance (e.g. spawned using
824
829
[ ` child_process.fork() ` ] ), the ` process.disconnect() ` method can be invoked
825
830
within the child process to close the IPC channel as well.
826
831
827
- ### child.kill([ signal] )
832
+ <a name =" child_process_child_kill_signal " ></a >
833
+ ### subprocess.kill([ signal] )
828
834
<!-- YAML
829
835
added: v0.1.90
830
836
-->
831
837
832
838
* ` signal ` {string}
833
839
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.
837
843
838
844
``` js
839
845
const spawn = require (' child_process' ).spawn ;
@@ -868,7 +874,7 @@ as in this example:
868
874
' use strict' ;
869
875
const spawn = require (' child_process' ).spawn ;
870
876
871
- const child = spawn (
877
+ const subprocess = spawn (
872
878
' sh' ,
873
879
[
874
880
' -c' ,
@@ -881,11 +887,12 @@ const child = spawn(
881
887
);
882
888
883
889
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
885
891
}, 2000 );
886
892
```
887
893
888
- ### child.pid
894
+ <a name =" child_process_child_pid " ></a >
895
+ ### subprocess.pid
889
896
<!-- YAML
890
897
added: v0.1.90
891
898
-->
@@ -904,7 +911,8 @@ console.log(`Spawned child pid: ${grep.pid}`);
904
911
grep .stdin .end ();
905
912
```
906
913
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 ] )
908
916
<!-- YAML
909
917
added: v0.5.9
910
918
-->
@@ -916,9 +924,10 @@ added: v0.5.9
916
924
* Returns: {boolean}
917
925
918
926
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.
922
931
923
932
For example, in the parent script:
924
933
@@ -954,8 +963,8 @@ for use within Node.js core and will not be emitted in the child's
954
963
Applications should avoid using such messages or listening for
955
964
` 'internalMessage' ` events as it is subject to change without notice.
956
965
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
959
968
receive the object as the second argument passed to the callback function
960
969
registered on the [ ` process.on('message') ` ] [ ] event. Any data that is received
961
970
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
976
985
` 'error' ` event will be emitted by the [ ` ChildProcess ` ] [ ] object. This can happen,
977
986
for instance, when the child process has already exited.
978
987
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
980
989
backlog of unsent messages exceeds a threshold that makes it unwise to send
981
990
more. Otherwise, the method returns ` true ` . The ` callback ` function can be
982
991
used to implement flow control.
@@ -987,15 +996,15 @@ The `sendHandle` argument can be used, for instance, to pass the handle of
987
996
a TCP server object to the child process as illustrated in the example below:
988
997
989
998
``` js
990
- const child = require (' child_process' ).fork (' child .js' );
999
+ const subprocess = require (' child_process' ).fork (' subprocess .js' );
991
1000
992
1001
// Open up the server object and send the handle.
993
1002
const server = require (' net' ).createServer ();
994
1003
server .on (' connection' , (socket ) => {
995
1004
socket .end (' handled by parent' );
996
1005
});
997
1006
server .listen (1337 , () => {
998
- child .send (' server' , server);
1007
+ subprocess .send (' server' , server);
999
1008
});
1000
1009
```
1001
1010
@@ -1026,8 +1035,8 @@ socket to the child process. The example below spawns two children that each
1026
1035
handle connections with "normal" or "special" priority:
1027
1036
1028
1037
``` 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' ]);
1031
1040
1032
1041
// Open up the server and send sockets to child
1033
1042
const server = require (' net' ).createServer ();
@@ -1044,8 +1053,8 @@ server.on('connection', (socket) => {
1044
1053
server .listen (1337 );
1045
1054
```
1046
1055
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:
1049
1058
1050
1059
``` js
1051
1060
process .on (' message' , (m , socket ) => {
@@ -1063,7 +1072,8 @@ this occurs.
1063
1072
* Note: this function uses [ ` JSON.stringify() ` ] [ ] internally to serialize the
1064
1073
` message ` .*
1065
1074
1066
- ### child.stderr
1075
+ <a name =" child_process_child_stderr " ></a >
1076
+ ### subprocess.stderr
1067
1077
<!-- YAML
1068
1078
added: v0.1.90
1069
1079
-->
@@ -1075,10 +1085,11 @@ A `Readable Stream` that represents the child process's `stderr`.
1075
1085
If the child was spawned with ` stdio[2] ` set to anything other than ` 'pipe' ` ,
1076
1086
then this will be ` null ` .
1077
1087
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.
1080
1090
1081
- ### child.stdin
1091
+ <a name =" child_process_child_stdin " ></a >
1092
+ ### subprocess.stdin
1082
1093
<!-- YAML
1083
1094
added: v0.1.90
1084
1095
-->
@@ -1093,10 +1104,11 @@ continue until this stream has been closed via `end()`.*
1093
1104
If the child was spawned with ` stdio[0] ` set to anything other than ` 'pipe' ` ,
1094
1105
then this will be ` null ` .
1095
1106
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.
1098
1109
1099
- ### child.stdio
1110
+ <a name =" child_process_child_stdio " ></a >
1111
+ ### subprocess.stdio
1100
1112
<!-- YAML
1101
1113
added: v0.7.10
1102
1114
-->
@@ -1105,38 +1117,39 @@ added: v0.7.10
1105
1117
1106
1118
A sparse array of pipes to the child process, corresponding with positions in
1107
1119
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.
1111
1123
1112
1124
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 ` .
1115
1127
1116
1128
``` js
1117
1129
const assert = require (' assert' );
1118
1130
const fs = require (' fs' );
1119
1131
const child_process = require (' child_process' );
1120
1132
1121
- const child = child_process .spawn (' ls' , {
1133
+ const subprocess = child_process .spawn (' ls' , {
1122
1134
stdio: [
1123
1135
0 , // Use parent's stdin for child
1124
1136
' pipe' , // Pipe child's stdout to parent
1125
1137
fs .openSync (' err.out' , ' w' ) // Direct child's stderr to a file
1126
1138
]
1127
1139
});
1128
1140
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 );
1131
1143
1132
- assert (child .stdout );
1133
- assert .strictEqual (child .stdio [1 ], child .stdout );
1144
+ assert (subprocess .stdout );
1145
+ assert .strictEqual (subprocess .stdio [1 ], subprocess .stdout );
1134
1146
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 );
1137
1149
```
1138
1150
1139
- ### child.stdout
1151
+ <a name =" child_process_child_stdout " ></a >
1152
+ ### subprocess.stdout
1140
1153
<!-- YAML
1141
1154
added: v0.1.90
1142
1155
-->
@@ -1148,8 +1161,8 @@ A `Readable Stream` that represents the child process's `stdout`.
1148
1161
If the child was spawned with ` stdio[1] ` set to anything other than ` 'pipe' ` ,
1149
1162
then this will be ` null ` .
1150
1163
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.
1153
1166
1154
1167
## ` maxBuffer ` and Unicode
1155
1168
@@ -1162,13 +1175,13 @@ to `stdout` although there are only 4 characters.
1162
1175
[ `'error'` ] : #child_process_event_error
1163
1176
[ `'exit'` ] : #child_process_event_exit
1164
1177
[ `'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
1172
1185
[ `child_process.exec()` ] : #child_process_child_process_exec_command_options_callback
1173
1186
[ `child_process.execFile()` ] : #child_process_child_process_execfile_file_args_options_callback
1174
1187
[ `child_process.execFileSync()` ] : #child_process_child_process_execfilesync_file_args_options
0 commit comments