Skip to content

Commit 781290b

Browse files
eljefedelrodeodeljefeMyles Borins
authored and
Myles Borins
committed
doc: refine child_process detach behaviour
this adds an example of a long running node process that actually executes node code. Also it mentions the not to harmonic detach behaviours of the different platforms, whereas detaching on unix requires ignoring the child_process' stdio explicitely. PR-URL: #5330 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8317778 commit 781290b

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

doc/api/child_process.markdown

+21-8
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,27 @@ Doing so will cause the parent's event loop to not include the child in its
405405
reference count, allowing the parent to exit independently of the child, unless
406406
there is an established IPC channel between the child and parent.
407407

408-
Example of detaching a long-running process and redirecting its output to a
409-
file:
408+
When using the `detached` option to start a long-running process, the process
409+
will not stay running in the background after the parent exits unless it is
410+
provided with a `stdio` configuration that is not connected to the parent.
411+
If the parent's `stdio` is inherited, the child will remain attached to the
412+
controlling terminal.
413+
414+
Example of a long-running process, by detaching and also ignoring its parent
415+
`stdio` file descriptors, in order to ignore the parent's termination:
416+
417+
```js
418+
const spawn = require('child_process').spawn;
419+
420+
const child = spawn(process.argv[0], ['child_program.js'], {
421+
detached: true,
422+
stdio: ['ignore']
423+
});
424+
425+
child.unref();
426+
```
427+
428+
Alternatively one can redirect the child process' output into files:
410429

411430
```js
412431
const fs = require('fs');
@@ -422,12 +441,6 @@ const child = spawn('prg', [], {
422441
child.unref();
423442
```
424443

425-
When using the `detached` option to start a long-running process, the process
426-
will not stay running in the background after the parent exits unless it is
427-
provided with a `stdio` configuration that is not connected to the parent.
428-
If the parent's `stdio` is inherited, the child will remain attached to the
429-
controlling terminal.
430-
431444
#### options.stdio
432445

433446
The `options.stdio` option is used to configure the pipes that are established

0 commit comments

Comments
 (0)