-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
child_process.spawn sh not killable #2098
Comments
I don't think that's a bug. Your example sends signals to The reason it works with |
But with That should be the point of the |
I think you have the wrong idea about processes and process groups. On exit, child processes get reparented, they're not forcibly killed by io.js or the kernel.
|
From the node docs: options.detached
What I read from that sentence: if detached = false the child get force killed
What you say is, that the child is always continuing after the parent exits, regardless of |
The documentation for
Not quite. /cc @nodejs/documentation - the documentation can probably be improved, although I'm not sure where to draw the line between inline documentation and pointing people to a UNIX process management primer. |
@paulpflug what exact platform were you using to test this? (any more specific than unix?) I found the same problem, works on mac but not on ubuntu. Seems to close properly if I use |
@Karissa ubuntu ;) I'm unsure of the reliability of bash currently I live with using if (process.platform != "win32") {
spawn("sh", ["-c", "kill -INT -"+child.pid]);
} else {
child.kill("SIGINT");
} I would really appreciate a more beautiful solution, but found none so far. |
@paulpflug If you want to send a signal to an entire process group, using detached (on non-windows) to put the child and its descendants in a group is exactly what unix process gouping is intended for. Your spawning of kill should be unnecessary, use |
@sam-github that is cool, didn't know that. And it does work 👍 I created a small wrapper: better-spawn |
This isn't Node specific, but it might be something worth mentioning in the documentation. |
@nodejs/documentation |
Though I agree with the general "processes on the different platforms" discussion, I think I can confirm a bug here: 'use strict'
const spawn = require('child_process').spawn
console.log('Spawning from:', process.pid)
let child = spawn('sh', ['-c',
`node -e "
process.on('SIGHUP', () => {console.log('received SIGHUP', process.exit())})
setInterval(() => {
console.log(process.pid, 'is alive')
}, 500);"`
], {
stdio: ['inherit', 'inherit', 'inherit']
})
child.on('close', () => {
console.log('caught child\'s exit')
// exiting here should be unecessary
process.exit()
})
setTimeout(() => {
// child.kill()
// child.kill('SIGINT')
// child.kill('SIGTERM')
// child.kill('SIGHUP')
console.log(child.pid)
process.kill(child.pid + 1, 'SIGHUP')
}, 2000) This has the following output. Spawning from: 32656
32662 'is alive'
32662 'is alive'
32662 'is alive'
32661
caught child's exit
|
Scratch what I said. It's the pid of For Here is the output of paralle+ 3466 1.0 0.1 637396 15988 pts/2 Sl+ 12:06 0:00 ./node app.js
paralle+ 3474 0.0 0.0 4440 656 pts/2 S+ 12:06 0:00 sh -c node -e " process.on('SIGHUP', () => {con
paralle+ 3475 0.5 0.1 641360 14988 pts/2 Sl+ 12:06 0:00 node -e process.on('SIGHUP', () => {console.lo
paralle+ 3519 0.0 0.0 22640 1312 pts/1 R+ 12:06 0:00 ps aux |
|
Closed by PR by @eljefedelrodeodeljefe 🎉 |
Ping, everyone interested I have sent in a PR at let clds = process.children();
// => [ 5598, 5601]
clds.forEach((pid) => {
process.kill(pid);
}); Happy for initial thoughts (in light of the problem here). |
@eljefedelrodeodeljefe can you link it here? |
Sure. See above also or here libuv/libuv#836 |
+1 for adding the .children() part to the docs and to the NodeJS API. |
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 <benjamingr@gmail.com> Closes: #2098
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 <benjamingr@gmail.com> Closes: #2098
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 <benjamingr@gmail.com> Closes: #2098
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 <benjamingr@gmail.com> Closes: #2098
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 <benjamingr@gmail.com> Closes: #2098
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 <benjamingr@gmail.com> Closes: #2098
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 <benjamingr@gmail.com> Closes: #2098
My god! Thank you so much for bringing this up, guys. I've spent countless hours trying to figure out why my child process didn't receive these events – it's fixed now. 🙏 |
Comming from here:
darkguy2008/parallelshell#22
(btw: Usecase for #1009)
Everytime I need to spawn a process I do it like this
The problem:
child
is unkillable on unix.Example in coffee-script:
(on windows it works fine with sh replaced by cmd)
Even when I exit the process with
process.exit()
, the child stays alive.Workaround I found:
Killing by pgid works, by pid works not. Sending ^C from console works also, I assume it uses pgid always.
The text was updated successfully, but these errors were encountered: