@@ -536,11 +536,12 @@ process will be made the leader of a new process group and session. Note that
536
536
child processes may continue running after the parent exits regardless of
537
537
whether they are detached or not. See setsid(2) for more information.
538
538
539
- By default, the parent will wait for the detached child to exit. To prevent
540
- the parent from waiting for a given ` subprocess ` , use the ` subprocess.unref() `
541
- method. Doing so will cause the parent's event loop to not include the child in
542
- its reference count, allowing the parent to exit independently of the child,
543
- unless there is an established IPC channel between the child and parent.
539
+ By default, the parent will wait for the detached child to exit. To prevent the
540
+ parent from waiting for a given ` subprocess ` to exit, use the
541
+ ` subprocess.unref() ` method. Doing so will cause the parent's event loop to not
542
+ include the child in its reference count, allowing the parent to exit
543
+ independently of the child, unless there is an established IPC channel between
544
+ the child and the parent.
544
545
545
546
When using the ` detached ` option to start a long-running process, the process
546
547
will not stay running in the background after the parent exits unless it is
@@ -1094,6 +1095,27 @@ console.log(`Spawned child pid: ${grep.pid}`);
1094
1095
grep .stdin .end ();
1095
1096
```
1096
1097
1098
+ ### subprocess.ref()
1099
+ <!-- YAML
1100
+ added: v0.7.10
1101
+ -->
1102
+
1103
+ Calling ` subprocess.ref() ` after making a call to ` subprocess.unref() ` will
1104
+ restore the removed reference count for the child process, forcing the parent
1105
+ to wait for the child to exit before exiting itself.
1106
+
1107
+ ``` js
1108
+ const { spawn } = require (' child_process' );
1109
+
1110
+ const subprocess = spawn (process .argv [0 ], [' child_program.js' ], {
1111
+ detached: true ,
1112
+ stdio: ' ignore'
1113
+ });
1114
+
1115
+ subprocess .unref ();
1116
+ subprocess .ref ();
1117
+ ```
1118
+
1097
1119
### subprocess.send(message[ , sendHandle[ , options]] [ , callback ] )
1098
1120
<!-- YAML
1099
1121
added: v0.5.9
@@ -1362,6 +1384,29 @@ then this will be `null`.
1362
1384
` subprocess.stdout ` is an alias for ` subprocess.stdio[1] ` . Both properties will
1363
1385
refer to the same value.
1364
1386
1387
+ ### subprocess.unref()
1388
+ <!-- YAML
1389
+ added: v0.7.10
1390
+ -->
1391
+
1392
+ By default, the parent will wait for the detached child to exit. To prevent the
1393
+ parent from waiting for a given ` subprocess ` to exit, use the
1394
+ ` subprocess.unref() ` method. Doing so will cause the parent's event loop to not
1395
+ include the child in its reference count, allowing the parent to exit
1396
+ independently of the child, unless there is an established IPC channel between
1397
+ the child and the parent.
1398
+
1399
+ ``` js
1400
+ const { spawn } = require (' child_process' );
1401
+
1402
+ const subprocess = spawn (process .argv [0 ], [' child_program.js' ], {
1403
+ detached: true ,
1404
+ stdio: ' ignore'
1405
+ });
1406
+
1407
+ subprocess .unref ();
1408
+ ```
1409
+
1365
1410
## ` maxBuffer ` and Unicode
1366
1411
1367
1412
The ` maxBuffer ` option specifies the largest number of bytes allowed on ` stdout `
0 commit comments