Skip to content

Commit c810ced

Browse files
Trottrvagg
authored andcommitted
doc: wrap child_process.md at 80 characters
PR-URL: #26141 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f4955fd commit c810ced

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

doc/api/child_process.md

+61-56
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Child Process
22

33
<!--introduced_in=v0.10.0-->
4-
<!--lint disable maximum-line-length-->
54

65
> Stability: 2 - Stable
76
@@ -44,10 +43,12 @@ and asynchronous alternatives to [`child_process.spawn()`][] and
4443
[`child_process.spawnSync()`][]. *Note that each of these alternatives are
4544
implemented on top of [`child_process.spawn()`][] or [`child_process.spawnSync()`][].*
4645

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.
5152
* [`child_process.fork()`][]: spawns a new Node.js process and invokes a
5253
specified module with an IPC communication channel established that allows
5354
sending messages between parent and child.
@@ -72,21 +73,22 @@ implement the Node.js [`EventEmitter`][] API, allowing the parent process to
7273
register listener functions that are called when certain events occur during
7374
the life cycle of the child process.
7475

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.
7879

7980
### Spawning `.bat` and `.cmd` files on Windows
8081

8182
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
9092
[`child_process.exec()`][] do). In any case, if the script filename contains
9193
spaces it needs to be quoted.
9294

@@ -267,12 +269,13 @@ changes:
267269
* Returns: {ChildProcess}
268270

269271
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()`][].
273275

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.
276279

277280
```js
278281
const { execFile } = require('child_process');
@@ -350,10 +353,10 @@ changes:
350353

351354
The `child_process.fork()` method is a special case of
352355
[`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.
357360

358361
It is important to keep in mind that spawned Node.js child processes are
359362
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:
608611
for fds 0 - 2 are also available as [`subprocess.stdin`][],
609612
[`subprocess.stdout`][] and [`subprocess.stderr`][], respectively.
610613
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.
616620

617621
Accessing the IPC channel fd in any way other than [`process.send()`][]
618622
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()`][].
670674
## Synchronous Process Creation
671675

672676
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
675679
spawned process exits.
676680

677681
Blocking calls like these are mostly useful for simplifying general-purpose
@@ -728,10 +732,10 @@ changes:
728732
* Returns: {Buffer|string} The stdout from the command.
729733

730734
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.
735739

736740
If the child process intercepts and handles the `SIGTERM` signal and
737741
does not exit, the parent process will still wait until the child process has
@@ -791,11 +795,11 @@ changes:
791795
* Returns: {Buffer|string} The stdout from the command.
792796

793797
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
799803
process has exited.*
800804

801805
If the process times out or has a non-zero exit code, this method ***will***
@@ -885,8 +889,8 @@ arbitrary command execution.**
885889
added: v2.2.0
886890
-->
887891

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.
890894

891895
Instances of `ChildProcess` are not intended to be created directly. Rather,
892896
use the [`child_process.spawn()`][], [`child_process.exec()`][],
@@ -964,8 +968,8 @@ added: v0.5.9
964968
* `sendHandle` {Handle} A [`net.Socket`][] or [`net.Server`][] object, or
965969
undefined.
966970

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.
969973

970974
The message goes through serialization and parsing. The resulting
971975
message might not be the same as what is originally sent.
@@ -1034,11 +1038,11 @@ grep.on('close', (code, signal) => {
10341038
grep.kill('SIGHUP');
10351039
```
10361040

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.
10421046

10431047
Note that while the function is called `kill`, the signal delivered to the
10441048
child process may not actually terminate the process.
@@ -1180,8 +1184,8 @@ process.on('message', (m) => {
11801184
process.send({ foo: 'bar', baz: NaN });
11811185
```
11821186

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.
11851189

11861190
There is a special case when sending a `{cmd: 'NODE_foo'}` message. Messages
11871191
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
12021206
single argument: `null` on success, or an [`Error`][] object on failure.
12031207

12041208
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.
12071211

12081212
`subprocess.send()` will return `false` if the channel has closed or when the
12091213
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.
12451249

12461250
While the example above uses a server created using the `net` module, `dgram`
12471251
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.
12501255

12511256
#### Example: sending a socket object
12521257

0 commit comments

Comments
 (0)