Skip to content

Commit

Permalink
doc: refine child_process detach behaviour
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
eljefedelrodeodeljefe authored and Myles Borins committed Apr 8, 2016
1 parent 56dda6f commit 505faf6
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions doc/api/child_process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,27 @@ Doing so will cause the parent's event loop to not include the child in its
reference count, allowing the parent to exit independently of the child, unless
there is an established IPC channel between the child and parent.

Example of detaching a long-running process and redirecting its output to a
file:
When using the `detached` option to start a long-running process, the process
will not stay running in the background after the parent exits unless it is
provided with a `stdio` configuration that is not connected to the parent.
If the parent's `stdio` is inherited, the child will remain attached to the
controlling terminal.

Example of a long-running process, by detaching and also ignoring its parent
`stdio` file descriptors, in order to ignore the parent's termination:

```js
const spawn = require('child_process').spawn;

const child = spawn(process.argv[0], ['child_program.js'], {
detached: true,
stdio: ['ignore']
});

child.unref();
```

Alternatively one can redirect the child process' output into files:

```js
const fs = require('fs');
Expand All @@ -403,12 +422,6 @@ const child = spawn('prg', [], {
child.unref();
```

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

#### options.stdio

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

0 comments on commit 505faf6

Please sign in to comment.