1
1
# Child Process
2
2
3
3
<!-- introduced_in=v0.10.0-->
4
- <!-- lint disable maximum-line-length-->
5
4
6
5
> Stability: 2 - Stable
7
6
@@ -44,10 +43,12 @@ and asynchronous alternatives to [`child_process.spawn()`][] and
44
43
[ ` child_process.spawnSync() ` ] [ ] . * Note that each of these alternatives are
45
44
implemented on top of [ ` child_process.spawn() ` ] [ ] or [ ` child_process.spawnSync() ` ] [ ] .*
46
45
47
- * [ ` child_process.exec() ` ] [ ] : spawns a shell and runs a command within that shell,
48
- passing the ` stdout ` and ` stderr ` to a callback function when complete.
49
- * [ ` child_process.execFile() ` ] [ ] : similar to [ ` child_process.exec() ` ] [ ] except that
50
- it spawns the command directly without first spawning a shell by default.
46
+ * [ ` child_process.exec() ` ] [ ] : spawns a shell and runs a command within that
47
+ shell, passing the ` stdout ` and ` stderr ` to a callback function when
48
+ complete.
49
+ * [ ` child_process.execFile() ` ] [ ] : similar to [ ` child_process.exec() ` ] [ ] except
50
+ that it spawns the command directly without first spawning a shell by
51
+ default.
51
52
* [ ` child_process.fork() ` ] [ ] : spawns a new Node.js process and invokes a
52
53
specified module with an IPC communication channel established that allows
53
54
sending messages between parent and child.
@@ -72,21 +73,22 @@ implement the Node.js [`EventEmitter`][] API, allowing the parent process to
72
73
register listener functions that are called when certain events occur during
73
74
the life cycle of the child process.
74
75
75
- The [ ` child_process.exec() ` ] [ ] and [ ` child_process.execFile() ` ] [ ] methods additionally
76
- allow for an optional ` callback ` function to be specified that is invoked
77
- when the child process terminates.
76
+ The [ ` child_process.exec() ` ] [ ] and [ ` child_process.execFile() ` ] [ ] methods
77
+ additionally allow for an optional ` callback ` function to be specified that is
78
+ invoked when the child process terminates.
78
79
79
80
### Spawning ` .bat ` and ` .cmd ` files on Windows
80
81
81
82
The importance of the distinction between [ ` child_process.exec() ` ] [ ] and
82
- [ ` child_process.execFile() ` ] [ ] can vary based on platform. On Unix-type operating
83
- systems (Unix, Linux, macOS) [ ` child_process.execFile() ` ] [ ] can be more efficient
84
- because it does not spawn a shell by default. On Windows, however, ` .bat ` and ` .cmd `
85
- files are not executable on their own without a terminal, and therefore cannot
86
- be launched using [ ` child_process.execFile() ` ] [ ] . When running on Windows, ` .bat `
87
- and ` .cmd ` files can be invoked using [ ` child_process.spawn() ` ] [ ] with the ` shell `
88
- option set, with [ ` child_process.exec() ` ] [ ] , or by spawning ` cmd.exe ` and passing
89
- the ` .bat ` or ` .cmd ` file as an argument (which is what the ` shell ` option and
83
+ [ ` child_process.execFile() ` ] [ ] can vary based on platform. On Unix-type
84
+ operating systems (Unix, Linux, macOS) [ ` child_process.execFile() ` ] [ ] can be
85
+ more efficient because it does not spawn a shell by default. On Windows,
86
+ however, ` .bat ` and ` .cmd ` files are not executable on their own without a
87
+ terminal, and therefore cannot be launched using [ ` child_process.execFile() ` ] [ ] .
88
+ When running on Windows, ` .bat ` and ` .cmd ` files can be invoked using
89
+ [ ` child_process.spawn() ` ] [ ] with the ` shell ` option set, with
90
+ [ ` child_process.exec() ` ] [ ] , or by spawning ` cmd.exe ` and passing the ` .bat ` or
91
+ ` .cmd ` file as an argument (which is what the ` shell ` option and
90
92
[ ` child_process.exec() ` ] [ ] do). In any case, if the script filename contains
91
93
spaces it needs to be quoted.
92
94
@@ -267,12 +269,13 @@ changes:
267
269
* Returns: {ChildProcess}
268
270
269
271
The ` child_process.execFile() ` function is similar to [ ` child_process.exec() ` ] [ ]
270
- except that it does not spawn a shell by default. Rather, the specified executable ` file `
271
- is spawned directly as a new process making it slightly more efficient than
272
- [ ` child_process.exec() ` ] [ ] .
272
+ except that it does not spawn a shell by default. Rather, the specified
273
+ executable ` file ` is spawned directly as a new process making it slightly more
274
+ efficient than [ ` child_process.exec() ` ] [ ] .
273
275
274
- The same options as [ ` child_process.exec() ` ] [ ] are supported. Since a shell is not
275
- spawned, behaviors such as I/O redirection and file globbing are not supported.
276
+ The same options as [ ` child_process.exec() ` ] [ ] are supported. Since a shell is
277
+ not spawned, behaviors such as I/O redirection and file globbing are not
278
+ supported.
276
279
277
280
``` js
278
281
const { execFile } = require (' child_process' );
@@ -350,10 +353,10 @@ changes:
350
353
351
354
The ` child_process.fork() ` method is a special case of
352
355
[ ` child_process.spawn() ` ] [ ] used specifically to spawn new Node.js processes.
353
- Like [ ` child_process.spawn() ` ] [ ] , a [ ` ChildProcess ` ] [ ] object is returned. The returned
354
- [ ` ChildProcess ` ] [ ] will have an additional communication channel built-in that
355
- allows messages to be passed back and forth between the parent and child. See
356
- [ ` subprocess.send() ` ] [ ] for details.
356
+ Like [ ` child_process.spawn() ` ] [ ] , a [ ` ChildProcess ` ] [ ] object is returned. The
357
+ returned [ ` ChildProcess ` ] [ ] will have an additional communication channel
358
+ built-in that allows messages to be passed back and forth between the parent and
359
+ child. See [ ` subprocess.send() ` ] [ ] for details.
357
360
358
361
It is important to keep in mind that spawned Node.js child processes are
359
362
independent of the parent with exception of the IPC communication channel
@@ -608,11 +611,12 @@ pipes between the parent and child. The value is one of the following:
608
611
for fds 0 - 2 are also available as [ ` subprocess.stdin ` ] [ ] ,
609
612
[ ` subprocess.stdout ` ] [ ] and [ ` subprocess.stderr ` ] [ ] , respectively.
610
613
2 . ` 'ipc' ` - Create an IPC channel for passing messages/file descriptors
611
- between parent and child. A [ ` ChildProcess ` ] [ ] may have at most * one* IPC stdio
612
- file descriptor. Setting this option enables the [ ` subprocess.send() ` ] [ ]
613
- method. If the child is a Node.js process, the presence of an IPC channel
614
- will enable [ ` process.send() ` ] [ ] and [ ` process.disconnect() ` ] [ ] methods,
615
- as well as [ ` 'disconnect' ` ] [ ] and [ ` 'message' ` ] [ ] events within the child.
614
+ between parent and child. A [ ` ChildProcess ` ] [ ] may have at most * one* IPC
615
+ stdio file descriptor. Setting this option enables the
616
+ [ ` subprocess.send() ` ] [ ] method. If the child is a Node.js process, the
617
+ presence of an IPC channel will enable [ ` process.send() ` ] [ ] and
618
+ [ ` process.disconnect() ` ] [ ] methods, as well as [ ` 'disconnect' ` ] [ ] and
619
+ [ ` 'message' ` ] [ ] events within the child.
616
620
617
621
Accessing the IPC channel fd in any way other than [ ` process.send() ` ] [ ]
618
622
or using the IPC channel with a child process that is not a Node.js instance
@@ -670,8 +674,8 @@ See also: [`child_process.exec()`][] and [`child_process.fork()`][].
670
674
## Synchronous Process Creation
671
675
672
676
The [ ` child_process.spawnSync() ` ] [ ] , [ ` child_process.execSync() ` ] [ ] , and
673
- [ ` child_process.execFileSync() ` ] [ ] methods are ** synchronous** and ** WILL** block
674
- the Node.js event loop, pausing execution of any additional code until the
677
+ [ ` child_process.execFileSync() ` ] [ ] methods are ** synchronous** and ** WILL**
678
+ block the Node.js event loop, pausing execution of any additional code until the
675
679
spawned process exits.
676
680
677
681
Blocking calls like these are mostly useful for simplifying general-purpose
@@ -728,10 +732,10 @@ changes:
728
732
* Returns: {Buffer|string} The stdout from the command.
729
733
730
734
The ` child_process.execFileSync() ` method is generally identical to
731
- [ ` child_process.execFile() ` ] [ ] with the exception that the method will not return
732
- until the child process has fully closed. When a timeout has been encountered
733
- and ` killSignal ` is sent, the method won't return until the process has
734
- completely exited.
735
+ [ ` child_process.execFile() ` ] [ ] with the exception that the method will not
736
+ return until the child process has fully closed. When a timeout has been
737
+ encountered and ` killSignal ` is sent, the method won't return until the process
738
+ has completely exited.
735
739
736
740
If the child process intercepts and handles the ` SIGTERM ` signal and
737
741
does not exit, the parent process will still wait until the child process has
@@ -791,11 +795,11 @@ changes:
791
795
* Returns: {Buffer|string} The stdout from the command.
792
796
793
797
The ` child_process.execSync() ` method is generally identical to
794
- [ ` child_process.exec() ` ] [ ] with the exception that the method will not return until
795
- the child process has fully closed. When a timeout has been encountered and
796
- ` killSignal ` is sent, the method won't return until the process has completely
797
- exited. * Note that if the child process intercepts and handles the ` SIGTERM `
798
- signal and doesn't exit, the parent process will wait until the child
798
+ [ ` child_process.exec() ` ] [ ] with the exception that the method will not return
799
+ until the child process has fully closed. When a timeout has been encountered
800
+ and ` killSignal ` is sent, the method won't return until the process has
801
+ completely exited. * Note that if the child process intercepts and handles the
802
+ ` SIGTERM ` signal and doesn't exit, the parent process will wait until the child
799
803
process has exited.*
800
804
801
805
If the process times out or has a non-zero exit code, this method *** will***
@@ -885,8 +889,8 @@ arbitrary command execution.**
885
889
added: v2.2.0
886
890
-->
887
891
888
- Instances of the ` ChildProcess ` class are [ ` EventEmitters ` ] [ `EventEmitter` ] that represent
889
- spawned child processes.
892
+ Instances of the ` ChildProcess ` class are [ ` EventEmitters ` ] [ `EventEmitter` ] that
893
+ represent spawned child processes.
890
894
891
895
Instances of ` ChildProcess ` are not intended to be created directly. Rather,
892
896
use the [ ` child_process.spawn() ` ] [ ] , [ ` child_process.exec() ` ] [ ] ,
@@ -964,8 +968,8 @@ added: v0.5.9
964
968
* ` sendHandle ` {Handle} A [ ` net.Socket ` ] [ ] or [ ` net.Server ` ] [ ] object, or
965
969
undefined.
966
970
967
- The ` 'message' ` event is triggered when a child process uses [ ` process.send() ` ] [ ]
968
- to send messages.
971
+ The ` 'message' ` event is triggered when a child process uses
972
+ [ ` process.send() ` ] [ ] to send messages.
969
973
970
974
The message goes through serialization and parsing. The resulting
971
975
message might not be the same as what is originally sent.
@@ -1034,11 +1038,11 @@ grep.on('close', (code, signal) => {
1034
1038
grep .kill (' SIGHUP' );
1035
1039
```
1036
1040
1037
- The [ ` ChildProcess ` ] [ ] object may emit an [ ` 'error' ` ] [ ] event if the signal cannot be
1038
- delivered. Sending a signal to a child process that has already exited is not
1039
- an error but may have unforeseen consequences. Specifically, if the process
1040
- identifier (PID) has been reassigned to another process, the signal will be
1041
- delivered to that process instead which can have unexpected results.
1041
+ The [ ` ChildProcess ` ] [ ] object may emit an [ ` 'error' ` ] [ ] event if the signal
1042
+ cannot be delivered. Sending a signal to a child process that has already exited
1043
+ is not an error but may have unforeseen consequences. Specifically, if the
1044
+ process identifier (PID) has been reassigned to another process, the signal will
1045
+ be delivered to that process instead which can have unexpected results.
1042
1046
1043
1047
Note that while the function is called ` kill ` , the signal delivered to the
1044
1048
child process may not actually terminate the process.
@@ -1180,8 +1184,8 @@ process.on('message', (m) => {
1180
1184
process .send ({ foo: ' bar' , baz: NaN });
1181
1185
```
1182
1186
1183
- Child Node.js processes will have a [ ` process.send() ` ] [ ] method of their own that
1184
- allows the child to send messages back to the parent.
1187
+ Child Node.js processes will have a [ ` process.send() ` ] [ ] method of their own
1188
+ that allows the child to send messages back to the parent.
1185
1189
1186
1190
There is a special case when sending a ` {cmd: 'NODE_foo'} ` message. Messages
1187
1191
containing a ` NODE_ ` prefix in the ` cmd ` property are reserved for use within
@@ -1202,8 +1206,8 @@ sent but before the child may have received it. The function is called with a
1202
1206
single argument: ` null ` on success, or an [ ` Error ` ] [ ] object on failure.
1203
1207
1204
1208
If no ` callback ` function is provided and the message cannot be sent, an
1205
- ` 'error' ` event will be emitted by the [ ` ChildProcess ` ] [ ] object. This can happen,
1206
- for instance, when the child process has already exited.
1209
+ ` 'error' ` event will be emitted by the [ ` ChildProcess ` ] [ ] object. This can
1210
+ happen, for instance, when the child process has already exited.
1207
1211
1208
1212
` subprocess.send() ` will return ` false ` if the channel has closed or when the
1209
1213
backlog of unsent messages exceeds a threshold that makes it unwise to send
@@ -1245,8 +1249,9 @@ can be handled by the parent and some by the child.
1245
1249
1246
1250
While the example above uses a server created using the ` net ` module, ` dgram `
1247
1251
module servers use exactly the same workflow with the exceptions of listening on
1248
- a ` 'message' ` event instead of ` 'connection' ` and using ` server.bind() ` instead of
1249
- ` server.listen() ` . This is, however, currently only supported on UNIX platforms.
1252
+ a ` 'message' ` event instead of ` 'connection' ` and using ` server.bind() ` instead
1253
+ of ` server.listen() ` . This is, however, currently only supported on UNIX
1254
+ platforms.
1250
1255
1251
1256
#### Example: sending a socket object
1252
1257
0 commit comments