Kill all child process of a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill all child process of a script
# 1  
Old 11-21-2012
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

Code:
#!/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
Code:
$ 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

Code:
#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:
Code:
$ 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
Code:
$ 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


Last edited by maaaaarco; 11-21-2012 at 06:24 PM..
# 2  
Old 11-21-2012
Shells are notorious for forking sub processes, you may have more luck using pkill

eg:
Code:
pkill -TERM -P $$

# 3  
Old 11-21-2012
Quote:
Originally Posted by maaaaarco
i want that ... all the child process and even the same script should be killed
Code:
#!/bin/bash

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

Last edited by alister; 11-21-2012 at 09:20 PM..
# 4  
Old 11-22-2012
Quote:
Originally Posted by alister
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
# 5  
Old 11-24-2012
Finally i decided to use kill 0 option and run my script in interactive mode, by putting this line in the .desktop file

Terminal=trueso 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 Smilie

See you man, and thx for help !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System should not kill the child process when parent id is 1

HI i would like to know how i can simulate a shell scripts for my requirement. example Server name child Process id Parent Process id Vpesh 16013 15637 Server name child Process id Parent Process id Vpesh 16014 15637 Server name child... (1 Reply)
Discussion started by: vpesh
1 Replies

2. Shell Programming and Scripting

How to kill a child script if it takes more than 10 minutes?

Hi all, I have a query on killing a child process, if it takes more than 10 minutes myparent.sh has the following #!/bin/sh echo "My Parent Script" home/guru/initiateServer.sh The initiateServer is a child process and this might take 20 or more minutes to return. I want to kill this... (11 Replies)
Discussion started by: guruincredible
11 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

5. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

6. Shell Programming and Scripting

Script to Kill a Process by Name...

Hello all... new to these forums and a bit of a newbie with linux aswell. I need to figure out how to write a shell script to kill a process by name as given to the script as an argument. I've got that part working OK, but i need to make sure that the script does not allow processes that are... (6 Replies)
Discussion started by: cannon1707
6 Replies

7. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

8. Shell Programming and Scripting

Script to kill process...

hello Bros, I need to write some script that i can put it on crontab which checks for a process X if running. If the process X is ruuning then take the PID and kill it or display message that says process X is not running. I am using AIX 5.3 Thanks guys.:b: (2 Replies)
Discussion started by: malcomex999
2 Replies

9. Shell Programming and Scripting

Script to kill process

Hello guys, I have a process named monitoreo, with 'monitoreo start' my process start until i kill them, now i want to do 'monitoreo stop' to kill them. After 'monitoreo start' i have this process running: ps -af UID PID PPID C STIME TTY TIME CMD ati 10958 1495 ... (5 Replies)
Discussion started by: Lestat
5 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question