Killing a bash script does not kill child processes
To kill a child process and all it's children you may use process.kill
with a negative pid
(to kill a process group)
var child = child_process.spawn('scripts/start-node.sh', {detached: true})
// Later…
process.kill(-child.pid, 'SIGKILL');
See details on child_process
documentation for options.detached
On non-Windows, if the detached option is set, the child process will be made the leader of a new process group and session.
Referencing here a portion of man 2 kill
for some details:
If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.
Another option may be using trap
in your shell script to intercept a signal and kill all the children and using child.kill('SIGTERM')
from node
(as SIGKILL
will not be intercepted by trap
)
#!/bin/bash
trap 'kill $(jobs -p)' EXIT
node_modules/.bin/babel-node --stage 0 index.js
Simply:
#!/bin/bash
if pgrep -x "node" > /dev/null
then
mv -f /usr/local/bin/node /usr/local/bin/node.1
killall node
mv -f /usr/local/bin/node.1 /usr/local/bin/node
which node
else
echo "process node not exists"
fi
node is creating child process every-time we kill it. So it's not possible to kill the process from kill,pkill or killall commands. So we are removing node command to make forking process fail and then we kill the process.Finally we restore the node command.