GeezerVison is showing github.com — test: test and docs for detached fork process · nodejs/node@5966dbe
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/child_process.md +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -325,6 +325,9 @@ changes: |
325 | 325 | * `args` {string[]} List of string arguments. |
326 | 326 | * `options` {Object} |
327 | 327 | * `cwd` {string} Current working directory of the child process. |
| 328 | + * `detached` {boolean} Prepare child to run independently of its parent |
| 329 | + process. Specific behavior depends on the platform, see |
| 330 | + [`options.detached`][]). |
328 | 331 | * `env` {Object} Environment key-value pairs. |
329 | 332 | * `execPath` {string} Executable used to create the child process. |
330 | 333 | * `execArgv` {string[]} List of string arguments passed to the executable. |
|
Collapse file
Copy file name to clipboard +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +const fork = require('child_process').fork; |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const child = fork( |
| 5 | + path.join(__dirname, 'child-process-persistent.js'), |
| 6 | + [], |
| 7 | + { detached: true, stdio: 'ignore' } |
| 8 | +); |
| 9 | + |
| 10 | +console.log(child.pid); |
| 11 | + |
| 12 | +child.unref(); |
Collapse file
Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const fork = require('child_process').fork; |
| 5 | +const fixtures = require('../common/fixtures'); |
| 6 | + |
| 7 | +const nonPersistentNode = fork( |
| 8 | + fixtures.path('parent-process-nonpersistent-fork.js'), |
| 9 | + [], |
| 10 | + { silent: true }); |
| 11 | + |
| 12 | +let childId = -1; |
| 13 | + |
| 14 | +nonPersistentNode.stdout.on('data', (data) => { |
| 15 | + childId = parseInt(data, 10); |
| 16 | + nonPersistentNode.kill(); |
| 17 | +}); |
| 18 | + |
| 19 | +process.on('exit', () => { |
| 20 | + assert.notStrictEqual(childId, -1); |
| 21 | + // Killing the child process should not throw an error |
| 22 | + process.kill(childId); |
| 23 | +}); |