Skip to content

Commit f56a805

Browse files
RaisinTendanielleadams
authored andcommitted
doc: refactor signal info in child_process.md
* Since exec calls execFile and execFile internally calls spawn with options.signal, the signal parameter has been documented under exec as well. * Refactor the description of signal under all the functions. * Add examples of usage of signal under all the functions and add missing requires in the other examples. PR-URL: #37528 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 33436e3 commit f56a805

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

doc/api/child_process.md

+50-6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ exec('"my script.cmd" a b', (err, stdout, stderr) => {
146146
<!-- YAML
147147
added: v0.1.90
148148
changes:
149+
- version: v15.4.0
150+
pr-url: https://github.com/nodejs/node/pull/36308
151+
description: AbortSignal support was added.
149152
- version: v8.8.0
150153
pr-url: https://github.com/nodejs/node/pull/15380
151154
description: The `windowsHide` option is supported now.
@@ -160,6 +163,8 @@ changes:
160163
* `shell` {string} Shell to execute the command with. See
161164
[Shell requirements][] and [Default Windows shell][]. **Default:**
162165
`'/bin/sh'` on Unix, `process.env.ComSpec` on Windows.
166+
* `signal` {AbortSignal} allows aborting the child process using an
167+
AbortSignal.
163168
* `timeout` {number} **Default:** `0`
164169
* `maxBuffer` {number} Largest amount of data in bytes allowed on stdout or
165170
stderr. If exceeded, the child process is terminated and any output is
@@ -183,6 +188,8 @@ directly by the shell and special characters (vary based on
183188
need to be dealt with accordingly:
184189

185190
```js
191+
const { exec } = require('child_process');
192+
186193
exec('"/path/to/test file/test.sh" arg1 arg2');
187194
// Double quotes are used so that the space in the path is not interpreted as
188195
// a delimiter of multiple arguments.
@@ -246,6 +253,20 @@ async function lsExample() {
246253
lsExample();
247254
```
248255

256+
If the `signal` option is enabled, calling `.abort()` on the corresponding
257+
`AbortController` is similar to calling `.kill()` on the child process except
258+
the error passed to the callback will be an `AbortError`:
259+
260+
```js
261+
const { exec } = require('child_process');
262+
const controller = new AbortController();
263+
const { signal } = controller;
264+
const child = exec('grep ssh', { signal }, (error) => {
265+
console.log(error); // an AbortError
266+
});
267+
controller.abort();
268+
```
269+
249270
### `child_process.execFile(file[, args][, options][, callback])`
250271
<!-- YAML
251272
added: v0.1.91
@@ -280,7 +301,8 @@ changes:
280301
`'/bin/sh'` on Unix, and `process.env.ComSpec` on Windows. A different
281302
shell can be specified as a string. See [Shell requirements][] and
282303
[Default Windows shell][]. **Default:** `false` (no shell).
283-
* `signal` {AbortSignal} allows aborting the execFile using an AbortSignal.
304+
* `signal` {AbortSignal} allows aborting the child process using an
305+
AbortSignal.
284306
* `callback` {Function} Called with the output when process terminates.
285307
* `error` {Error}
286308
* `stdout` {string|Buffer}
@@ -339,6 +361,7 @@ If the `signal` option is enabled, calling `.abort()` on the corresponding
339361
the error passed to the callback will be an `AbortError`:
340362

341363
```js
364+
const { execFile } = require('child_process');
342365
const controller = new AbortController();
343366
const { signal } = controller;
344367
const child = execFile('node', ['--version'], { signal }, (error) => {
@@ -385,7 +408,8 @@ changes:
385408
* `serialization` {string} Specify the kind of serialization used for sending
386409
messages between processes. Possible values are `'json'` and `'advanced'`.
387410
See [Advanced serialization][] for more details. **Default:** `'json'`.
388-
* `signal` {AbortSignal} Allows closing the subprocess using an AbortSignal.
411+
* `signal` {AbortSignal} Allows closing the child process using an
412+
AbortSignal.
389413
* `killSignal` {string} The signal value to be used when the spawned
390414
process will be killed by the abort signal. **Default:** `'SIGTERM'`.
391415
* `silent` {boolean} If `true`, stdin, stdout, and stderr of the child will be
@@ -429,8 +453,26 @@ current process.
429453
The `shell` option available in [`child_process.spawn()`][] is not supported by
430454
`child_process.fork()` and will be ignored if set.
431455

432-
The `signal` option works exactly the same way it does in
433-
[`child_process.spawn()`][].
456+
If the `signal` option is enabled, calling `.abort()` on the corresponding
457+
`AbortController` is similar to calling `.kill()` on the child process except
458+
the error passed to the callback will be an `AbortError`:
459+
460+
```js
461+
if (process.argv[2] === 'child') {
462+
setTimeout(() => {
463+
console.log(`Hello from ${process.argv[2]}!`);
464+
}, 1_000);
465+
} else {
466+
const { fork } = require('child_process');
467+
const controller = new AbortController();
468+
const { signal } = controller;
469+
const child = fork(__filename, ['child'], { signal });
470+
child.on('error', (err) => {
471+
// This will be called with err being an AbortError if the controller aborts
472+
});
473+
controller.abort(); // Stops the child process
474+
}
475+
```
434476

435477
### `child_process.spawn(command[, args][, options])`
436478
<!-- YAML
@@ -484,7 +526,8 @@ changes:
484526
when `shell` is specified and is CMD. **Default:** `false`.
485527
* `windowsHide` {boolean} Hide the subprocess console window that would
486528
normally be created on Windows systems. **Default:** `false`.
487-
* `signal` {AbortSignal} allows aborting the execFile using an AbortSignal.
529+
* `signal` {AbortSignal} allows aborting the child process using an
530+
AbortSignal.
488531
* `killSignal` {string} The signal value to be used when the spawned
489532
process will be killed by the abort signal. **Default:** `'SIGTERM'`.
490533

@@ -599,13 +642,14 @@ If the `signal` option is enabled, calling `.abort()` on the corresponding
599642
the error passed to the callback will be an `AbortError`:
600643

601644
```js
645+
const { spawn } = require('child_process');
602646
const controller = new AbortController();
603647
const { signal } = controller;
604648
const grep = spawn('grep', ['ssh'], { signal });
605649
grep.on('error', (err) => {
606650
// This will be called with err being an AbortError if the controller aborts
607651
});
608-
controller.abort(); // stops the process
652+
controller.abort(); // Stops the child process
609653
```
610654

611655
#### `options.detached`

0 commit comments

Comments
 (0)