@@ -527,11 +527,12 @@ process will be made the leader of a new process group and session. Note that |
527 | 527 | child processes may continue running after the parent exits regardless of |
528 | 528 | whether they are detached or not. See setsid(2) for more information. |
529 | 529 |
|
530 | | -By default, the parent will wait for the detached child to exit. To prevent |
531 | | -the parent from waiting for a given `subprocess`, use the `subprocess.unref()` |
532 | | -method. Doing so will cause the parent's event loop to not include the child in |
533 | | -its reference count, allowing the parent to exit independently of the child, |
534 | | -unless there is an established IPC channel between the child and parent. |
| 530 | +By default, the parent will wait for the detached child to exit. To prevent the |
| 531 | +parent from waiting for a given `subprocess` to exit, use the |
| 532 | +`subprocess.unref()` method. Doing so will cause the parent's event loop to not |
| 533 | +include the child in its reference count, allowing the parent to exit |
| 534 | +independently of the child, unless there is an established IPC channel between |
| 535 | +the child and the parent. |
535 | 536 |
|
536 | 537 | When using the `detached` option to start a long-running process, the process |
537 | 538 | 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}`); |
1076 | 1077 | grep.stdin.end(); |
1077 | 1078 | ``` |
1078 | 1079 |
|
| 1080 | +### subprocess.ref() |
| 1081 | +<!-- YAML |
| 1082 | +added: v0.7.10 |
| 1083 | +--> |
| 1084 | + |
| 1085 | +Calling `subprocess.ref()` after making a call to `subprocess.unref()` will |
| 1086 | +restore the removed reference count for the child process, forcing the parent |
| 1087 | +to wait for the child to exit before exiting itself. |
| 1088 | + |
| 1089 | +```js |
| 1090 | +const { spawn } = require('child_process'); |
| 1091 | + |
| 1092 | +const subprocess = spawn(process.argv[0], ['child_program.js'], { |
| 1093 | + detached: true, |
| 1094 | + stdio: 'ignore' |
| 1095 | +}); |
| 1096 | + |
| 1097 | +subprocess.unref(); |
| 1098 | +subprocess.ref(); |
| 1099 | +``` |
| 1100 | + |
1079 | 1101 | ### subprocess.send(message[, sendHandle[, options]][, callback]) |
1080 | 1102 | <!-- YAML |
1081 | 1103 | added: v0.5.9 |
@@ -1344,6 +1366,29 @@ then this will be `null`. |
1344 | 1366 | `subprocess.stdout` is an alias for `subprocess.stdio[1]`. Both properties will |
1345 | 1367 | refer to the same value. |
1346 | 1368 |
|
| 1369 | +### subprocess.unref() |
| 1370 | +<!-- YAML |
| 1371 | +added: v0.7.10 |
| 1372 | +--> |
| 1373 | + |
| 1374 | +By default, the parent will wait for the detached child to exit. To prevent the |
| 1375 | +parent from waiting for a given `subprocess` to exit, use the |
| 1376 | +`subprocess.unref()` method. Doing so will cause the parent's event loop to not |
| 1377 | +include the child in its reference count, allowing the parent to exit |
| 1378 | +independently of the child, unless there is an established IPC channel between |
| 1379 | +the child and the parent. |
| 1380 | + |
| 1381 | +```js |
| 1382 | +const { spawn } = require('child_process'); |
| 1383 | + |
| 1384 | +const subprocess = spawn(process.argv[0], ['child_program.js'], { |
| 1385 | + detached: true, |
| 1386 | + stdio: 'ignore' |
| 1387 | +}); |
| 1388 | + |
| 1389 | +subprocess.unref(); |
| 1390 | +``` |
| 1391 | + |
1347 | 1392 | ## `maxBuffer` and Unicode |
1348 | 1393 |
|
1349 | 1394 | The `maxBuffer` option specifies the largest number of bytes allowed on `stdout` |
|