GeezerVison is showing github.com — doc: describe child.kill() pitfalls on linux · nodejs/node@59814ac
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.markdown +24-2Lines changed: 24 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -750,7 +750,29 @@ delivered to that process instead which can have unexpected results. |
750 | 750 | Note that while the function is called `kill`, the signal delivered to the |
751 | 751 | child process may not actually terminate the process. |
752 | 752 |
|
753 | | -See `kill(2)` |
| 753 | +See `kill(2)` for reference. |
| 754 | + |
| 755 | +Also note: on Linux, child processes of child processes will not be terminated |
| 756 | +when attempting to kill their parent. This is likely to happen when running a |
| 757 | +new process in a shell or with use of the `shell` option of `ChildProcess`, such |
| 758 | +as in this example: |
| 759 | + |
| 760 | +```js |
| 761 | +'use strict'; |
| 762 | +const spawn = require('child_process').spawn; |
| 763 | + |
| 764 | +let child = spawn('sh', ['-c', |
| 765 | + `node -e "setInterval(() => { |
| 766 | + console.log(process.pid + 'is alive') |
| 767 | + }, 500);"` |
| 768 | + ], { |
| 769 | + stdio: ['inherit', 'inherit', 'inherit'] |
| 770 | + }); |
| 771 | + |
| 772 | +setTimeout(() => { |
| 773 | + child.kill(); // does not terminate the node process in the shell |
| 774 | +}, 2000); |
| 775 | +``` |
754 | 776 |
|
755 | 777 | ### child.pid |
756 | 778 |
|
@@ -1007,4 +1029,4 @@ to the same value. |
1007 | 1029 | [`options.stdio`]: #child_process_options_stdio |
1008 | 1030 | [`stdio`]: #child_process_options_stdio |
1009 | 1031 | [synchronous counterparts]: #child_process_synchronous_process_creation |
1010 | | -[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |
| 1032 | +[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |