Kill all child processes on trap


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Kill all child processes on trap
# 1  
Old 06-11-2008
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 processes.

I tried
Code:
ps -ef | grep <processname> | awk '{print $2}' | xargs kill -9

but it will kill not only the processes started by the running script but also the other processes.

please help me out.

Thank you.

Last edited by rakeshou; 06-11-2008 at 09:42 PM.. Reason: specified OS
# 2  
Old 06-12-2008
You can track in a file or in an array all the PIDs of the background processes started by the main script ($!) and kill all of them when SIGINT is received.

Look at this example shell script to get an idea:
Code:
trap 'kill_em_all' 2

kill_em_all() {
   echo "Kill 'em all!"
   kill -9 `cat /tmp/kill.lst`
}

sleep 40 &
echo $!  > /tmp/kill.lst
sleep 50 &
echo $! >> /tmp/kill.lst
sleep 60 &
echo $! >> /tmp/kill.lst

wait

# 3  
Old 06-12-2008
As a side note, unless you are really sure you know what you are doing, you should avoid kill -9 -- google for "useless use of kill -9"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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. > cat ./parent #!/bin/ksh while do . ./child arg1 & if ; then break fi done Is there a way to get the process group id for all the child... (3 Replies)
Discussion started by: midhun19
3 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

trap kill -9

Is there a way to trap kill -9 pid? or is there any other way to detect kill -9 so that some files can be deleted before the process ends? (3 Replies)
Discussion started by: sindhu001
3 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. 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

6. Shell Programming and Scripting

Trap not working in orphaned child processes

I've search the various posts in these forums, but have not come up with a solution to my problem. I have a parent process that calls a child script, runs it in the background and the parent finishes - without waiting for the child process to complete. Inside the child, a trap is issued to trap... (6 Replies)
Discussion started by: HobieCoop
6 Replies

7. Shell Programming and Scripting

How to Trap kill -9 signal

I just want to trap kill -9 signal issued by any of user from any terminal and just capture that user terminal who had raised this kill -9 command (1 Reply)
Discussion started by: puneet.goel
1 Replies

8. Shell Programming and Scripting

Getting exit status of child in trap handler

Hi, I have a trap problem when calling a child script in the background. I know there are a lot of threads here on the issue of traps and signals, I think I have read all the relevant ones, but still haven't found an answer to my problem. I'm working on Linux or HP, the script as you can see... (4 Replies)
Discussion started by: rimon
4 Replies

9. Shell Programming and Scripting

Over-riding TRAP in Child

Hi, I'm using Ksh on HP 10.2. My parent shell script has ignored INT signal using trap command. trap "" 2 3 .... (other signals) This script calls another script in which INT signal should be active and should not be ignored. I browsed the net and found out that in ksh, once a... (1 Reply)
Discussion started by: anijog
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