Skip to content

Commit 5faeb7e

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 25d44f7 commit 5faeb7e

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
@@ -750,7 +750,29 @@ delivered to that process instead which can have unexpected results.
750750
Note that while the function is called `kill`, the signal delivered to the
751751
child process may not actually terminate the process.
752752

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+
```
754776

755777
### child.pid
756778

@@ -1007,4 +1029,4 @@ to the same value.
10071029
[`options.stdio`]: #child_process_options_stdio
10081030
[`stdio`]: #child_process_options_stdio
10091031
[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

0 commit comments

Comments
 (0)