Kill all child process of a script

Hi guys i have a problem with a script... this script creates differents GUI with YAD... well i want that when i press the "Cancel" button on this graphical interface all the child process and even the same script should be killed

#!/bin/bash

function gui_start {
    local choice=""
    choice=`yad --title="TITLE" --center --text="WELCOME insert text" --entry`
    verify_exit "$?"
    
    if [ "$choice" == "a" ]; then
        gui="gui_1"
    else
        gui="gui_1"
    fi

    "$gui" | fun_1 | yad --title="This is the list" --center --list --column="First":TEXT
}

function gui_1 {
    local x=""
    x=`yad --text="Insert a number" --entry`
    verify_exit $?
    
    if [ "$x" == "" ]; then
        exit
    fi
    
    while [ $x -ne 0 ]
    do
        echo "aaa bbb"
        x=$(($x-1))
    done
}

function fun_1 {
    while read input
    do
        echo "$input"
    done
}

function verify_exit {
    local code="$1"
    if [ "$code" -eq 1 -o "$code" -eq 252 ]; then
        kill_all_child $MY_PID
    fi
}

function kill_all_child {
    local parent=$1
    local childs=""

    local com="ps -Al | grep -w $parent | awk '{print \$4}'"
    childs=`eval $com`

echo "process pid: $parent"
echo "childs: $childs"

    local j=0

        for i in $childs
        do
            if [ $j -ne 0 ]; then #Skip the first pid in $childs that is the current process id
                kill_all_child "$i"
            fi
            j=$(($j+1))
        done

    kill -kill $parent
}

MY_PID=$$
gui_start

The point is this:
If you click "cancel" in the 2nd and 3rd interface that will come out (the two after the one asking to insert the text) one or the other will not be killed...

I put here some more info for you... after click Ok on the first situation this is the situation

$ ps -Al
...
0 S  1002 21515 21512  0  80   0 - 28022 wait   pts/3    00:00:00 my-search-tool
1 S  1002 21519 21515  0  80   0 - 28022 pipe_w pts/3    00:00:00 my-search-tool
1 S  1002 21520 21515  0  80   0 - 28022 pipe_w pts/3    00:00:00 my-search-tool
0 S  1002 21521 21515  0  80   0 - 104391 poll_s pts/3   00:00:00 yad
0 S  1002 21522 21519  1  80   0 - 104357 poll_s pts/3   00:00:00 yad

...

And this is the output when i click on the Cancel button on the interface asking you to insert a number

#First call to KILL_ALL_CHILDS
process pid: 21515
childs: 21515 21519 21520 21521

#Second call
process pid: 21519
childs: 21519 21598 #Why 21598 ? Is not in the output of ps -Al of before ?! And why there is not 21522 ??

#Third call
process pid: 21598
childs: 

#And here i get the error:
./my-search-tool-2: line 67: kill: (21598) - No such process

Can anyone help me ? Thx

P.S.
If in the same situation as before i call another script from a different shell, where i have launch my script, that do the same thing as KILL_ALL_CHILDS it works fine...
This is the situation:

$ ps -Al
0 S  1002 24241 24184  0  80   0 - 28022 wait   pts/1    00:00:00 my-search-tool
1 S  1002 24243 24241  0  80   0 - 28022 pipe_w pts/1    00:00:00 my-search-tool
1 S  1002 24244 24241  0  80   0 - 28022 pipe_w pts/1    00:00:00 my-search-tool
0 S  1002 24245 24241  1  80   0 - 104428 poll_s pts/1   00:00:00 yad
0 S  1002 24246 24243  0  80   0 - 104357 poll_s pts/1   00:00:00 yad

And this is the output

$ EXTERNAL_KILL_ALL_CHILD 24241
mypid: 24241
childs: 24241 24243 24244 24245

mypid: 24243
childs: 24243 24246

mypid: 24246
childs: 24246

mypid: 24244
childs: 

Desktop/EXTERNAL_KILL_ALL_CHILDS: line 20: kill: (24244) - No such process

mypid: 24245
childs: 24245

Shells are notorious for forking sub processes, you may have more luck using pkill

eg:

pkill -TERM -P $$

That shell is almost certainly not running in interactive mode. If that's so, then all of its children and its children's children and so forth are running in the same process group. You can consult with ps to verify (your ps output above does not include pgrp info, as far as I can tell).

From within any process in a process group, kill 0 signals all cohorts, whether they be parent, child, grandchild, sibling, cousin, etc.

Regards,
Alister

What do you mean by Interactive ?

I tried with kill -0, but i run this script from the Utility menu of my Fedora 17, i add a .desktop file in /usr/share/applications, and when execute kill -0 just kill all process in the current system session :S

Finally i decided to use kill 0 option and run my script in interactive mode, by putting this line in the .desktop file

Terminal=true so when my script execute the kill 0 command does not kill all the current system process....

If someone can get ride of it please let me know :wink:

See you man, and thx for help !