From fafdae4ce1758ac4f4fbc181f3c9d87a6b99a2b5 Mon Sep 17 00:00:00 2001 From: Thomas Hunter II Date: Thu, 9 Aug 2018 14:46:07 -0700 Subject: [PATCH] doc: add subprocess.ref() and subprocess.unref() PR-URL: https://github.com/nodejs/node/pull/22220 Reviewed-By: Anna Henningsen Reviewed-By: Bryan English Reviewed-By: James M Snell Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Colin Ihrig Reviewed-By: Vse Mozhet Byt --- doc/api/child_process.md | 55 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index e419d9693d13eb..795f581decd2f4 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -527,11 +527,12 @@ process will be made the leader of a new process group and session. Note that child processes may continue running after the parent exits regardless of whether they are detached or not. See setsid(2) for more information. -By default, the parent will wait for the detached child to exit. To prevent -the parent from waiting for a given `subprocess`, use the `subprocess.unref()` -method. 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. +By default, the parent will wait for the detached child to exit. To prevent the +parent from waiting for a given `subprocess` to exit, use the +`subprocess.unref()` method. 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 the parent. 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 @@ -1076,6 +1077,27 @@ console.log(`Spawned child pid: ${grep.pid}`); grep.stdin.end(); ``` +### subprocess.ref() + + +Calling `subprocess.ref()` after making a call to `subprocess.unref()` will +restore the removed reference count for the child process, forcing the parent +to wait for the child to exit before exiting itself. + +```js +const { spawn } = require('child_process'); + +const subprocess = spawn(process.argv[0], ['child_program.js'], { + detached: true, + stdio: 'ignore' +}); + +subprocess.unref(); +subprocess.ref(); +``` + ### subprocess.send(message[, sendHandle[, options]][, callback]) + +By default, the parent will wait for the detached child to exit. To prevent the +parent from waiting for a given `subprocess` to exit, use the +`subprocess.unref()` method. 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 the parent. + +```js +const { spawn } = require('child_process'); + +const subprocess = spawn(process.argv[0], ['child_program.js'], { + detached: true, + stdio: 'ignore' +}); + +subprocess.unref(); +``` + ## `maxBuffer` and Unicode The `maxBuffer` option specifies the largest number of bytes allowed on `stdout`