@@ -400,10 +400,10 @@ Example of checking for failed exec:
400
400
401
401
``` js
402
402
const spawn = require (' child_process' ).spawn ;
403
- const child = spawn (' bad_command' );
403
+ const subprocess = spawn (' bad_command' );
404
404
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 .' );
407
407
});
408
408
```
409
409
@@ -423,10 +423,10 @@ child processes may continue running after the parent exits regardless of
423
423
whether they are detached or not. See ` setsid(2) ` for more information.
424
424
425
425
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.
430
430
431
431
When using the ` detached ` option to start a long-running process, the process
432
432
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
440
440
``` js
441
441
const spawn = require (' child_process' ).spawn ;
442
442
443
- const child = spawn (process .argv [0 ], [' child_program.js' ], {
443
+ const subprocess = spawn (process .argv [0 ], [' child_program.js' ], {
444
444
detached: true ,
445
445
stdio: ' ignore'
446
446
});
447
447
448
- child .unref ();
448
+ subprocess .unref ();
449
449
```
450
450
451
451
Alternatively one can redirect the child process' output into files:
@@ -456,12 +456,12 @@ const spawn = require('child_process').spawn;
456
456
const out = fs .openSync (' ./out.log' , ' a' );
457
457
const err = fs .openSync (' ./out.log' , ' a' );
458
458
459
- const child = spawn (' prg' , [], {
459
+ const subprocess = spawn (' prg' , [], {
460
460
detached: true ,
461
461
stdio: [ ' ignore' , out, err ]
462
462
});
463
463
464
- child .unref ();
464
+ subprocess .unref ();
465
465
```
466
466
467
467
#### options.stdio
@@ -471,9 +471,10 @@ added: v0.7.10
471
471
472
472
The ` options.stdio ` option is used to configure the pipes that are established
473
473
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'] `.
477
478
478
479
For convenience, ` options.stdio ` may be one of the following strings:
479
480
@@ -763,46 +764,49 @@ added: v0.5.9
763
764
The ` 'message' ` event is triggered when a child process uses ` process.send() `
764
765
to send messages.
765
766
766
- ### child.connected
767
+ <a name =" child_process_child_connected " ></a >
768
+ ### subprocess.connected
767
769
<!-- YAML
768
770
added: v0.7.2
769
771
-->
770
772
771
773
* {Boolean} Set to false after ` .disconnect ` is called
772
774
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.
776
778
777
- ### child.disconnect()
779
+ <a name =" child_process_child_disconnect " ></a >
780
+ ### subprocess.disconnect()
778
781
<!-- YAML
779
782
added: v0.7.2
780
783
-->
781
784
782
785
Closes the IPC channel between parent and child, allowing the child to exit
783
786
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.
787
790
788
791
The ` 'disconnect' ` event will be emitted when there are no messages in the
789
792
process of being received. This will most often be triggered immediately after
790
- calling ` child .disconnect()` .
793
+ calling ` subprocess .disconnect()` .
791
794
792
795
Note that when the child process is a Node.js instance (e.g. spawned using
793
796
[ ` child_process.fork() ` ] ), the ` process.disconnect() ` method can be invoked
794
797
within the child process to close the IPC channel as well.
795
798
796
- ### child.kill([ signal] )
799
+ <a name =" child_process_child_kill_signal " ></a >
800
+ ### subprocess.kill([ signal] )
797
801
<!-- YAML
798
802
added: v0.1.90
799
803
-->
800
804
801
805
* ` signal ` {String}
802
806
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.
806
810
807
811
``` js
808
812
const spawn = require (' child_process' ).spawn ;
@@ -837,7 +841,7 @@ as in this example:
837
841
' use strict' ;
838
842
const spawn = require (' child_process' ).spawn ;
839
843
840
- let child = spawn (' sh' , [' -c' ,
844
+ let subprocess = spawn (' sh' , [' -c' ,
841
845
` node -e "setInterval(() => {
842
846
console.log(process.pid, 'is alive')
843
847
}, 500);"`
@@ -846,11 +850,12 @@ let child = spawn('sh', ['-c',
846
850
});
847
851
848
852
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
850
854
}, 2000 );
851
855
```
852
856
853
- ### child.pid
857
+ <a name =" child_process_child_pid " ></a >
858
+ ### subprocess.pid
854
859
<!-- YAML
855
860
added: v0.1.90
856
861
-->
@@ -869,7 +874,8 @@ console.log(`Spawned child pid: ${grep.pid}`);
869
874
grep .stdin .end ();
870
875
```
871
876
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 ] )
873
879
<!-- YAML
874
880
added: v0.5.9
875
881
-->
@@ -880,9 +886,10 @@ added: v0.5.9
880
886
* Returns: {Boolean}
881
887
882
888
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.
886
893
887
894
For example, in the parent script:
888
895
@@ -918,8 +925,8 @@ for use within Node.js core and will not be emitted in the child's
918
925
Applications should avoid using such messages or listening for
919
926
` 'internalMessage' ` events as it is subject to change without notice.
920
927
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
923
930
receive the object as the second argument passed to the callback function
924
931
registered on the ` process.on('message') ` event. Any data that is received and
925
932
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
932
939
` 'error' ` event will be emitted by the ` ChildProcess ` object. This can happen,
933
940
for instance, when the child process has already exited.
934
941
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
936
943
backlog of unsent messages exceeds a threshold that makes it unwise to send
937
944
more. Otherwise, the method returns ` true ` . The ` callback ` function can be
938
945
used to implement flow control.
@@ -943,15 +950,15 @@ The `sendHandle` argument can be used, for instance, to pass the handle of
943
950
a TCP server object to the child process as illustrated in the example below:
944
951
945
952
``` js
946
- const child = require (' child_process' ).fork (' child .js' );
953
+ const subprocess = require (' child_process' ).fork (' subprocess .js' );
947
954
948
955
// Open up the server object and send the handle.
949
956
const server = require (' net' ).createServer ();
950
957
server .on (' connection' , (socket ) => {
951
958
socket .end (' handled by parent' );
952
959
});
953
960
server .listen (1337 , () => {
954
- child .send (' server' , server);
961
+ subprocess .send (' server' , server);
955
962
});
956
963
```
957
964
@@ -982,8 +989,8 @@ socket to the child process. The example below spawns two children that each
982
989
handle connections with "normal" or "special" priority:
983
990
984
991
``` 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' ]);
987
994
988
995
// Open up the server and send sockets to child
989
996
const server = require (' net' ).createServer ();
@@ -1000,8 +1007,8 @@ server.on('connection', (socket) => {
1000
1007
server .listen (1337 );
1001
1008
```
1002
1009
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:
1005
1012
1006
1013
``` js
1007
1014
process .on (' message' , (m , socket ) => {
@@ -1018,7 +1025,8 @@ this occurs.
1018
1025
1019
1026
* Note: this function uses [ ` JSON.stringify() ` ] [ ] internally to serialize the ` message ` .*
1020
1027
1021
- ### child.stderr
1028
+ <a name =" child_process_child_stderr " ></a >
1029
+ ### subprocess.stderr
1022
1030
<!-- YAML
1023
1031
added: v0.1.90
1024
1032
-->
@@ -1030,10 +1038,11 @@ A `Readable Stream` that represents the child process's `stderr`.
1030
1038
If the child was spawned with ` stdio[2] ` set to anything other than ` 'pipe' ` ,
1031
1039
then this will be ` undefined ` .
1032
1040
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.
1035
1043
1036
- ### child.stdin
1044
+ <a name =" child_process_child_stdin " ></a >
1045
+ ### subprocess.stdin
1037
1046
<!-- YAML
1038
1047
added: v0.1.90
1039
1048
-->
@@ -1048,10 +1057,11 @@ continue until this stream has been closed via `end()`.*
1048
1057
If the child was spawned with ` stdio[0] ` set to anything other than ` 'pipe' ` ,
1049
1058
then this will be ` undefined ` .
1050
1059
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.
1053
1062
1054
- ### child.stdio
1063
+ <a name =" child_process_child_stdio " ></a >
1064
+ ### subprocess.stdio
1055
1065
<!-- YAML
1056
1066
added: v0.7.10
1057
1067
-->
@@ -1060,38 +1070,39 @@ added: v0.7.10
1060
1070
1061
1071
A sparse array of pipes to the child process, corresponding with positions in
1062
1072
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.
1066
1076
1067
1077
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 ` .
1070
1080
1071
1081
``` js
1072
1082
const assert = require (' assert' );
1073
1083
const fs = require (' fs' );
1074
1084
const child_process = require (' child_process' );
1075
1085
1076
- const child = child_process .spawn (' ls' , {
1086
+ const subprocess = child_process .spawn (' ls' , {
1077
1087
stdio: [
1078
1088
0 , // Use parents stdin for child
1079
1089
' pipe' , // Pipe child's stdout to parent
1080
1090
fs .openSync (' err.out' , ' w' ) // Direct child's stderr to a file
1081
1091
]
1082
1092
});
1083
1093
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 );
1086
1096
1087
- assert (child .stdout );
1088
- assert .equal (child .stdio [1 ], child .stdout );
1097
+ assert (subprocess .stdout );
1098
+ assert .equal (subprocess .stdio [1 ], subprocess .stdout );
1089
1099
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 );
1092
1102
```
1093
1103
1094
- ### child.stdout
1104
+ <a name =" child_process_child_stdout " ></a >
1105
+ ### subprocess.stdout
1095
1106
<!-- YAML
1096
1107
added: v0.1.90
1097
1108
-->
@@ -1103,8 +1114,8 @@ A `Readable Stream` that represents the child process's `stdout`.
1103
1114
If the child was spawned with ` stdio[1] ` set to anything other than ` 'pipe' ` ,
1104
1115
then this will be ` undefined ` .
1105
1116
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.
1108
1119
1109
1120
[ `popen(3)` ] : http://linux.die.net/man/3/popen
1110
1121
[ `ChildProcess` ] : #child_process_child_process
0 commit comments