Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Kill all child process of a script

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-21-2012
maaaaarco maaaaarco is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 7 March 2014, 11:21 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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 05:24 PM..
Sponsored Links
    #2  
Old Unix and Linux 11-21-2012
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 10 September 2017, 9:33 PM EDT
Posts: 3,414
Thanks: 144
Thanked 1,209 Times in 1,109 Posts
Shells are notorious for forking sub processes, you may have more luck using pkill

eg:

Code:
pkill -TERM -P $$

Sponsored Links
    #3  
Old Unix and Linux 11-21-2012
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 976 Times in 790 Posts
Quote:
Originally Posted by maaaaarco View Post
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 08:20 PM..
    #4  
Old Unix and Linux 11-22-2012
maaaaarco maaaaarco is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 7 March 2014, 11:21 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by alister View Post
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
Sponsored Links
    #5  
Old Unix and Linux 11-24-2012
maaaaarco maaaaarco is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 7 March 2014, 11:21 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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 Linux

See you man, and thx for help !
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
forking a child process and kill its parent to show that child process has init() as its parent vizz_k Shell Programming and Scripting 2 11-17-2011 12:03 AM
script to get child process for a process schippada Shell Programming and Scripting 5 08-26-2011 05:53 PM
Script to start background process and then kill process holocene UNIX for Dummies Questions & Answers 4 06-10-2010 03:39 AM
Shell Script to Kill Process(number of process) Unix/Solaris jonnyvic Shell Programming and Scripting 2 12-03-2009 01:44 PM
Script to kill all child process for a given PID sanjay92 UNIX for Dummies Questions & Answers 4 03-21-2002 02:28 PM



All times are GMT -4. The time now is 11:41 PM.