Skip to content

Commit 5e6915f

Browse files
eljefedelrodeodeljefeMyles Borins
authored and
Myles Borins
committed
doc: describe child.kill() pitfalls on linux
This commit refines the documentation around child.kill(), where kill attempts against shells will lead to unexpected results. Namely, on linux the child process of a child process will not terminate, when its parent gets terminated. This is different across the the platforms. PR-URL: #2098 Reviewed-By: Benjamin Gruenbaum <[email protected]> Closes: #2098
1 parent 69eb4a6 commit 5e6915f

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

doc/api/child_process.markdown

+24-2
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,29 @@ delivered to that process instead which can have unexpected results.
759759
Note that while the function is called `kill`, the signal delivered to the
760760
child process may not actually terminate the process.
761761

762-
See `kill(2)`
762+
See `kill(2)` for reference.
763+
764+
Also note: on Linux, child processes of child processes will not be terminated
765+
when attempting to kill their parent. This is likely to happen when running a
766+
new process in a shell or with use of the `shell` option of `ChildProcess`, such
767+
as in this example:
768+
769+
```js
770+
'use strict';
771+
const spawn = require('child_process').spawn;
772+
773+
let child = spawn('sh', ['-c',
774+
`node -e "setInterval(() => {
775+
console.log(process.pid + 'is alive')
776+
}, 500);"`
777+
], {
778+
stdio: ['inherit', 'inherit', 'inherit']
779+
});
780+
781+
setTimeout(() => {
782+
child.kill(); // does not terminate the node process in the shell
783+
}, 2000);
784+
```
763785

764786
### child.pid
765787

@@ -1025,4 +1047,4 @@ to the same value.
10251047
[`options.stdio`]: #child_process_options_stdio
10261048
[`stdio`]: #child_process_options_stdio
10271049
[synchronous counterparts]: #child_process_synchronous_process_creation
1028-
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
1050+
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

0 commit comments

Comments
 (0)