Kill child processes, when parent is "bash"


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Kill child processes, when parent is "bash"
# 1  
Old 12-02-2009
Error Kill child processes, when parent is "bash"

Consider this simple command line
Code:
bash -c  'echo $$ ; sleep 10000'

This will print the newly created bash PID and sleep for a long time.
If I go to another terminal and do something like
Code:
ps -flax | grep [s]leep

I'll see something like
Code:
501 92418 91910   0   0:00.00 ttys000    0:00.00 bash -c echo $$ ; sleep 10000                                       4002  31  0    75964    708 -      S+   c8be82c
501 92419 92418   0   0:00.00 ttys000    0:00.00 sleep 10000                                                         4002  31  0    75412    380 -      S+   affa338

If I do kill then the parent (bash) process
Code:
kill 92418

then the child process (sleep) is not killed, although bash got killed

How do I kill the whole tree?
# 2  
Old 12-02-2009
So you could kill the child process (which has the PPID = bash's PID) before you kill bash.
# 3  
Old 12-02-2009
I know that I can programmatically parse the whole tree and recursively kill all children.
But it seems to me that this is has a big overhead.

Isn't there any other way to do it?
I thought this was the default behaviour, the childer to be killed when the parent gets killed.
# 4  
Old 12-03-2009
Actually, killing a parent process will usually, but not always, kill its children as well !
# 5  
Old 12-04-2009
Thank you for your answer, I thought this was the rule.
In any case I solved the problem with another way.
Thanks again
# 6  
Old 03-05-2010
Here a script to kill recursive, I named it rkill.sh, it search for PPIDs recursively using pgrep:

Code:
#!/bin/bash

### Current PID, if this script try to self search, then will loop forever
pid=$$

### Recursive function
search()
{
    if [ $1 == $pid ]; then
        break
    fi

    pgrep -P $1 |
    while read process; do
        echo $process ### Print PID to stdout for debug, replace by your kill command (kill -9, pkill, etc)
        search $process
    done
}

search $1


Last edited by wagnerluis1982; 03-05-2010 at 03:15 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

What is the use of "finger" command & how to use it to kill the online processes ?

Hi there, I am eager to know what exactly is the use of "finger" command & how to use it to kill the online processes ? :b: (1 Reply)
Discussion started by: abhijitpaul0212
1 Replies

3. Shell Programming and Scripting

Kill Parent/ Child processes

I am trying to kill PIDs that are tied to a KSH "load_sqlplus" and I am using the below code LIST_PID=`ps -ef | grep -i "load_sqlplus" | grep -v grep | awk '{print $2}'` if ; then echo "Processes killed" "PID : " $LIST_PID kill -9 $LIST_PID else echo "Nothing to Kill" fi... (4 Replies)
Discussion started by: venky338
4 Replies

4. 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

5. Shell Programming and Scripting

need to kill a number of processes with name "XYZ" at a time using shell script

Hi, when i grep for the process "XYZ" , there will be some good number of processes with that name, i want to kill all the these processes at a time using shell script? Any help needed for this action. Thanks Regards, Anil (6 Replies)
Discussion started by: anilmanepu
6 Replies

6. Shell Programming and Scripting

will child processes be created when executing "ps"?

Hi I'm trying to write some code to confirm there is only one running instance in memory like below: /usr/ucb/ps -auxww | egrep -v 'grep |vi |tail |more |cat ' | egrep ${SCRIPT_NAME} | egrep -v " \-h| \-help| \-v" But sometimes i found there is some child processes are are created as... (4 Replies)
Discussion started by: sleepy_11
4 Replies

7. AIX

Typing "bash" at the command line spawns two bash processes

Server: IBM p770 OS: AIX 6.1 TL5 SP1 When one of our develoeprs types "bash" on the command line to switch shells, it hangs. For some reason, two bash processes are created....the first bash process spawns a second bash process in the same console, causing a hang. Anyone have any idea what... (2 Replies)
Discussion started by: wjssj
2 Replies

8. Programming

"No child processes" and waitpif

Hi everybody, i'm using a signal handler for the SIGCHLD signal. void InstallNewSigChldHandler() { struct sigaction act; struct sigaction oldAct; act.sa_handler = CallWaitChildProcess; sigemptyset(&act.sa_mask); act.sa_flags = SA_NOCLDSTOP; if... (5 Replies)
Discussion started by: Zipi
5 Replies

9. UNIX for Advanced & Expert Users

"kill -14 pid" doesn't works on all processes !!

If I try to run "kill -14 pid", some processes in my application get terminated , while some keeps running. If SIGALRM signal is sent, they should make an exit. What's the reason any process keeps on running. (1 Reply)
Discussion started by: poojac
1 Replies

10. UNIX for Dummies Questions & Answers

kill parent and child

Hello all, I have gone through the search and looked at posting about idle users and killing processes. Here is my question I would like to kill an idle user ( which I can do) but how can I asure that all of his process is also killed whit out tracing his inital start PID. I have tried this on a... (4 Replies)
Discussion started by: larry
4 Replies
Login or Register to Ask a Question