Bash: Killing all processes in subprocess Bash: Killing all processes in subprocess unix unix

Bash: Killing all processes in subprocess


You can set a trap in the subshell to kill any active jobs before exiting:

 (trap 'kill $(jobs -p)' EXIT; sleep 5; echo done ) & pid=$!


I don't know exactly why that sleep process gets orphaned, anyway instead kill you can use pkill with -P flag to also kill all children

pkill -TERM -P $pid

EDIT:that means that in order to kill a process and all it's children you should use instead

CPIDS=`pgrep -P $pid` # gets pids of child processeskill -9 $pidfor cpid in $CPIDS ; do kill -9 $cpid ; done


You can have a look at rkill that seems to meet your requirements :

http://www.unix.com/man-page/debian/1/rkill/

rkill [-SIG] pid/name...

When invoked as rkill, this utility does not display information about the processes, but sends them all a signal instead. If not specified on the command line, a terminate (SIGTERM) signal is sent.