child_process.spawn sh not killable
Comming from here:
darkguy2008/parallelshell#22
(btw: Usecase for #1009)
Everytime I need to spawn a process I do it like this
if (process.platform === 'win32') { sh = 'cmd'; shFlag = '/c'; } else { sh = 'sh'; shFlag = '-c'; } var child = spawn(sh,[shFlag,cmd], { cwd: process.cwd, env: process.env, stdio: ['pipe', process.stdout, process.stderr] })
The problem: child is unkillable on unix.
Example in coffee-script:
spawn = require("child_process").spawn child = spawn "sh", ["-c", "node -e 'setTimeout(function(){},10000);'"] child.on "close", process.exit child.kill() child.kill("SIGINT") child.kill("SIGTERM") child.kill("SIGHUP") spawn "sh",["-c","kill -TERM "+child.pid] spawn "sh",["-c","kill -INT "+child.pid] spawn "sh",["-c","kill -HUP "+child.pid]
(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:
spawn = require("child_process").spawn child = spawn "sh", ["-c", "node -e 'setTimeout(function(){},10000);'"], detached: true child.on "close", process.exit spawn "sh",["-c","kill -INT -"+child.pid]
Killing by pgid works, by pid works not. Sending ^C from console works also, I assume it uses pgid always.