I have scriptA which will execute another script which will startup in the background. Now I need to make sure that when I kill scriptA (cmd+c) that the background processes are also killed.
#!/bin/bash
echo "This script is about to run another script."
sh ../processes/processb/bin/startserver.sh &
FOO_PID=$!
echo "This script has just run another script." $FOO_PID
This script executes fine, but once I press cmd+c and do a 'ps' command on FOO_PID value , that process still exists. What am I doing wrong?
UPDATE-----------
So I tried out below code, but still scriptC's process is not getting killed. I think it just only terminates scriptA ( parent) when pressed ctrl+c and therefore trap command does not get executed?
#!/bin/bash
echo "This script is about to run another script."
../common/samples/bin/scriptC.sh &
mypid=$!
kill -0 "$mypid" && echo "My process is still alive."
echo "This script has just run another script." $mypid
trap "kill $mypid && kill $$" INT