Kill child processes when exit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill child processes when exit
# 1  
Old 09-11-2013
Kill child processes when exit

Hi,
I have parent script which is invoking multiple child scripts. I would want to kill all the child processes before the parent
process exit.
Code:
 
> cat ./parent
#!/bin/ksh
while [ condition ]
do
. ./child arg1 &
if [ condition ]; then
break
fi
done

Is there a way to get the process group id for all the child and child sub processes and kill them all ?
# 2  
Old 09-11-2013
If its invoking multiple children then its own PID should be the common PPID of all children...
e.g. here I know I opened all my windows from one, I look for the first and ps grep its PID:
Code:
n12:/home/vbe/test/cut $ ps -ef|grep dtterm
     vbe 63242698 20250744   0 18:01:29 pts/12  0:00 grep dtterm
     vbe 65470862 54985206   1   Aug 08  pts/6 10:04 dtterm
n12:/home/vbe/test/cut $ ps -ef|grep 65470862|grep -v grep
     vbe 13238478 65470862   0   Aug 08  pts/7  0:00 /usr/dt/bin/dtksh
     vbe 16449648 65470862   0   Sep 04  pts/5  0:00 /usr/dt/bin/dtksh
     vbe 18677870 65470862   0   Aug 08  pts/3  0:00 /usr/dt/bin/dtksh
     vbe 20250744 65470862   1   Aug 13 pts/12  0:01 /usr/dt/bin/dtksh
     vbe 22610134 65470862   0   Aug 29  pts/0  0:00 /usr/dt/bin/dtksh
     vbe 38142314 65470862   0   Aug 13 pts/13  0:00 /usr/dt/bin/dtksh
     vbe 43581906 65470862   0   Aug 23  pts/2  0:00 dtksh
     vbe 52035856 65470862   0   Aug 13 pts/11  0:00 /usr/dt/bin/dtksh
     vbe 61800890 65470862   0   Aug 08  pts/8  0:00 /usr/dt/bin/dtksh
     vbe 62718426 65470862   0   Aug 13 pts/10  0:00 /usr/dt/bin/dtksh
     vbe 65470862 54985206   2   Aug 08  pts/6 10:04 dtterm

This User Gave Thanks to vbe For This Post:
# 3  
Old 09-11-2013
Quote:
Originally Posted by vbe
If its invoking multiple children then its own PID should be the common PPID of all children...
e.g. here I know I opened all my windows from one, I look for the first and ps grep its PID:
Code:
n12:/home/vbe/test/cut $ ps -ef|grep dtterm
     vbe 63242698 20250744   0 18:01:29 pts/12  0:00 grep dtterm
     vbe 65470862 54985206   1   Aug 08  pts/6 10:04 dtterm
n12:/home/vbe/test/cut $ ps -ef|grep 65470862|grep -v grep
     vbe 13238478 65470862   0   Aug 08  pts/7  0:00 /usr/dt/bin/dtksh
     vbe 16449648 65470862   0   Sep 04  pts/5  0:00 /usr/dt/bin/dtksh
     vbe 18677870 65470862   0   Aug 08  pts/3  0:00 /usr/dt/bin/dtksh
     vbe 20250744 65470862   1   Aug 13 pts/12  0:01 /usr/dt/bin/dtksh
     vbe 22610134 65470862   0   Aug 29  pts/0  0:00 /usr/dt/bin/dtksh
     vbe 38142314 65470862   0   Aug 13 pts/13  0:00 /usr/dt/bin/dtksh
     vbe 43581906 65470862   0   Aug 23  pts/2  0:00 dtksh
     vbe 52035856 65470862   0   Aug 13 pts/11  0:00 /usr/dt/bin/dtksh
     vbe 61800890 65470862   0   Aug 08  pts/8  0:00 /usr/dt/bin/dtksh
     vbe 62718426 65470862   0   Aug 13 pts/10  0:00 /usr/dt/bin/dtksh
     vbe 65470862 54985206   2   Aug 08  pts/6 10:04 dtterm

thanks
# 4  
Old 09-11-2013
kill with a pid of zero will signal every process in the current process group.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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="" ... (4 Replies)
Discussion started by: maaaaarco
4 Replies

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

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. UNIX for Dummies Questions & Answers

Kill child processes, when parent is "bash"

Consider this simple command line 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 ps -flax | grep leepI'll see something like 501 92418 91910 0 0:00.00 ttys000 0:00.00 bash -c echo $$... (5 Replies)
Discussion started by: teras
5 Replies

5. Programming

Why does my child process not exit?

Im sure it has something to do with the wait() call, but everything ive tried either leaves me with a zombie or with the exec executing indefinitely. switch(pid = fork()) { case -1:perror("fork failed"); exit(1); case 0: if(key == "cd") { execl("/bin/cd", "cd",... (2 Replies)
Discussion started by: p00ndawg
2 Replies

6. UNIX for Dummies Questions & Answers

Exit from n th child shell

Hi, I am using ksh to write my shell script. I need to create multiple-level of nested sub shells in my script. Lets say I have at n th subshell. My question is how do I come out from there to main login shell. If I use 'exit' command then it is exiting from just one subshell and back to... (4 Replies)
Discussion started by: indra_saha
4 Replies

7. UNIX for Advanced & Expert Users

Kill all child processes on trap

hi OS: Sun Solaris I have a scenario that when someone presses ctrl-c while executing a shell script, it should not just exit. it should kill all the child processes started by the running shell script only. I am executing many other scripts parallely which in turn fork off more... (2 Replies)
Discussion started by: rakeshou
2 Replies

8. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

9. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 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