@@ -146,6 +146,9 @@ exec('"my script.cmd" a b', (err, stdout, stderr) => {
146
146
<!-- YAML
147
147
added: v0.1.90
148
148
changes:
149
+ - version: v15.4.0
150
+ pr-url: https://github.com/nodejs/node/pull/36308
151
+ description: AbortSignal support was added.
149
152
- version: v8.8.0
150
153
pr-url: https://github.com/nodejs/node/pull/15380
151
154
description: The `windowsHide` option is supported now.
@@ -160,6 +163,8 @@ changes:
160
163
* ` shell ` {string} Shell to execute the command with. See
161
164
[ Shell requirements] [ ] and [ Default Windows shell] [ ] . ** Default:**
162
165
` '/bin/sh' ` on Unix, ` process.env.ComSpec ` on Windows.
166
+ * ` signal ` {AbortSignal} allows aborting the child process using an
167
+ AbortSignal.
163
168
* ` timeout ` {number} ** Default:** ` 0 `
164
169
* ` maxBuffer ` {number} Largest amount of data in bytes allowed on stdout or
165
170
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
183
188
need to be dealt with accordingly:
184
189
185
190
``` js
191
+ const { exec } = require (' child_process' );
192
+
186
193
exec (' "/path/to/test file/test.sh" arg1 arg2' );
187
194
// Double quotes are used so that the space in the path is not interpreted as
188
195
// a delimiter of multiple arguments.
@@ -246,6 +253,20 @@ async function lsExample() {
246
253
lsExample ();
247
254
```
248
255
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
+
249
270
### ` child_process.execFile(file[, args][, options][, callback]) `
250
271
<!-- YAML
251
272
added: v0.1.91
@@ -280,7 +301,8 @@ changes:
280
301
` '/bin/sh' ` on Unix, and ` process.env.ComSpec ` on Windows. A different
281
302
shell can be specified as a string. See [ Shell requirements] [ ] and
282
303
[ 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.
284
306
* ` callback ` {Function} Called with the output when process terminates.
285
307
* ` error ` {Error}
286
308
* ` stdout ` {string|Buffer}
@@ -339,6 +361,7 @@ If the `signal` option is enabled, calling `.abort()` on the corresponding
339
361
the error passed to the callback will be an ` AbortError ` :
340
362
341
363
``` js
364
+ const { execFile } = require (' child_process' );
342
365
const controller = new AbortController ();
343
366
const { signal } = controller;
344
367
const child = execFile (' node' , [' --version' ], { signal }, (error ) => {
@@ -385,7 +408,8 @@ changes:
385
408
* ` serialization ` {string} Specify the kind of serialization used for sending
386
409
messages between processes. Possible values are ` 'json' ` and ` 'advanced' ` .
387
410
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.
389
413
* ` killSignal ` {string} The signal value to be used when the spawned
390
414
process will be killed by the abort signal. ** Default:** ` 'SIGTERM' ` .
391
415
* ` silent ` {boolean} If ` true ` , stdin, stdout, and stderr of the child will be
@@ -429,8 +453,26 @@ current process.
429
453
The ` shell ` option available in [ ` child_process.spawn() ` ] [ ] is not supported by
430
454
` child_process.fork() ` and will be ignored if set.
431
455
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
+ ```
434
476
435
477
### ` child_process.spawn(command[, args][, options]) `
436
478
<!-- YAML
@@ -484,7 +526,8 @@ changes:
484
526
when ` shell ` is specified and is CMD. ** Default:** ` false ` .
485
527
* ` windowsHide ` {boolean} Hide the subprocess console window that would
486
528
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.
488
531
* ` killSignal ` {string} The signal value to be used when the spawned
489
532
process will be killed by the abort signal. ** Default:** ` 'SIGTERM' ` .
490
533
@@ -599,13 +642,14 @@ If the `signal` option is enabled, calling `.abort()` on the corresponding
599
642
the error passed to the callback will be an ` AbortError ` :
600
643
601
644
``` js
645
+ const { spawn } = require (' child_process' );
602
646
const controller = new AbortController ();
603
647
const { signal } = controller;
604
648
const grep = spawn (' grep' , [' ssh' ], { signal });
605
649
grep .on (' error' , (err ) => {
606
650
// This will be called with err being an AbortError if the controller aborts
607
651
});
608
- controller .abort (); // stops the process
652
+ controller .abort (); // Stops the child process
609
653
```
610
654
611
655
#### ` options.detached `
0 commit comments