Killing process and children


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing process and children
# 1  
Old 01-28-2009
Killing process and children

Hi all,

I have been searching all day for a nice solution to this problem.

I have three scripts. A start script, a child script and a stop script.

Script A (scripta.sh)
Its Child Script B (scriptb.sh)
Script C (kill_process.sh $PID)

Script A correctly traps the kill command sent from Script C as can be seen in the code below.

Code:
echo "In Script B"
trap 'killed_exit' INT TERM EXIT
echo "Starting Script B, lets hope it gets killed when I am killed"
./scriptb.sh #problem here because scriptb.sh takes a while, and script a sits and waits for script b to finish even script a was told to die, aswell as it doesn't actually send a kill to scriptb.sh, it continues running through its lines
echo "Finished Running Script B"
echo "Finished Script A"

The problem is, is that scriptb.sh may take an hour to run, and script A when it receives the kill command, will sit there and wait until ./scriptb.sh finishes.

Is there a way I can get script a to immediately kill all of its children, and get out of there, instead of the two problems I have

1) Not killing scriptb.sh
2) Waiting for scriptb.sh

Regards,
# 2  
Old 01-28-2009
how about this:

. ./scriptb.sh

Last edited by quirkasaurus; 01-28-2009 at 12:01 PM.. Reason: yes, it works like you want.
# 3  
Old 01-28-2009
Wow thats a great idea.... but what I was really hoping to do eventually was

instead of

Code:
.
.
./scriptb.sh
.
.

I will be doign

Code:
.
.
rsh machineb "/scriptb.sh"
.
.

I guess this one isn't so easy to source or kill as its being run remotely?
# 4  
Old 01-28-2009
The way to do this is indeed tricky. But here's how:

The remote machine script echoes its process-id immediately into a text file
on the remote machine.

The calling script traps the signal...

and calls a remote shell like so:

Code:
function killem_all
{
rsh machineb "kill -9 `cat process-id-file`"
}

trap 'killem_all' INT TERM EXIT


Last edited by quirkasaurus; 01-28-2009 at 12:07 PM.. Reason: some quoting problems
# 5  
Old 02-02-2009
Hi

Thank you but the problem here is, my main calling script that begins the rsh, when it receives the kill command, it actually sits and waits for the rsh to fully complete before it enters the killemall function.

If the rsh command takes one hour, then the kill will take one hour to start the killemall function.

Any ideas on how to get around this? I am sure it is designed like this, ie finish the line of code currently being run before running the code specified in "trap". Unfortunately this is not desirable for me as I need it to quit immediately without finishing the current line aka rsh machineb........
# 6  
Old 02-02-2009
Quote:
Originally Posted by mark007
Hi

Thank you but the problem here is, my main calling script that begins the rsh, when it receives the kill command, it actually sits and waits for the rsh to fully complete before it enters the killemall function.

If the rsh command takes one hour, then the kill will take one hour to start the killemall function.

Any ideas on how to get around this? I am sure it is designed like this, ie finish the line of code currently being run before running the code specified in "trap". Unfortunately this is not desirable for me as I need it to quit immediately without finishing the current line aka rsh machineb........

Did you try my second suggestion?

I tried fooling around with this concept:

On the remote machine, the main script is called "clam":

Code:
echo $$ > pid
while : ; do
date
sleep 10
done > date_log

On the remote machine, there's a kill-script called "duck":

Code:
(
echo looking for clam process
ps -deaf | fgrep clam
echo killing it:
kill -2 `cat pid`
kill -3 `cat pid`
kill -9 `cat pid`
echo checking again:
ps -deaf | fgrep clam
echo done
) > duck.log 2>&1

On the main host, there's a script called "fish":

Code:
trap 'echo trapped signal; rsh sun03 duck; exit 1' 2 3
rsh sun03 'clam &' &
echo back from sun03
echo waiting now...
wait

I think this covers everything.
# 7  
Old 02-03-2009
wow that looks good, I think the idea of using & and wait was excellent to get around the blocking.

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing the process ID's

Hi , I have a list of application process id's. Is there a way to kill all the process listed below using the script, except the once which are starting with " Genesis " adm 1522 ABC_Process.tra adm 1939 Genesis_Process.tra adm 2729 Genesis_Archive.tra adm 3259 xyz_Process.tra (5 Replies)
Discussion started by: murali1687
5 Replies

2. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

3. Shell Programming and Scripting

Killing process!!!!

Hi friends, i m in big trouble.... i have one script which connects two server ...like below.. script1.sh ------------------------------------- bash test.sh & eval x=$@ export x=`echo $x` #echo $x # ssh user@8.2.5.6 bash /mbbv/location/script.sh $x|sed '/Binary file/d'... (1 Reply)
Discussion started by: Shahul
1 Replies

4. UNIX for Dummies Questions & Answers

killing the process

Hi, First, I am running a scipt.While the script is running I realize that I dont want the script to be run so I am killing the script externally.Before the process gets terminated or killed it should delete all the temporary files created by the script.How to do this?Can anyone help me? ... (3 Replies)
Discussion started by: arthi
3 Replies

5. Shell Programming and Scripting

Killing of a process and send a mail if the process doesnot come up within 2 minutes

Hi Friends, I am new to this forum as well as new to shell scripting. I have a problem here and i need someone to solve this. Let us consider there are two processes(abc & def).There is a script which kills these two processes(i.e killtheprocess abc). Here abc is the argument . There is a... (1 Reply)
Discussion started by: Prince89
1 Replies

6. UNIX for Dummies Questions & Answers

killing a process

I can kill running processes on my linux red hat system using ctrl-c but cannot do it from command line of another terminal using kill -2 pid. Although I can kill them from command line using kill -9 pid and other signals. I would like to do it using the kill -2 pid. Thanks for your suggestions (6 Replies)
Discussion started by: bbhayana
6 Replies

7. Shell Programming and Scripting

killing a child process

I am calling another script from my main script and making it run in the background,based upon the value of the input provided by the user I want to kill the child process ,I have written this code timer.ksh & PID=$$ print "\n Do you wish to continue .. (Y/N) : \c " read kill_proc if ]... (4 Replies)
Discussion started by: mervin2006
4 Replies

8. AIX

killing a process from a script

Hey all. I'm brand new to this forum and am looking for some help. I have a script that verifies that the backup tapes are working correctly. Basically is uses 1 command: restore -xpqvf > rootvglog I use this for each volume group that we have. We run this everyday but the problem is, we... (4 Replies)
Discussion started by: jalge2
4 Replies

9. Shell Programming and Scripting

killing process using a script

can I do ps -ef | grep <process_name> and kill the process is it exists? and send a mail to me that the process was found and killed Thanks much... KS (4 Replies)
Discussion started by: skotapal
4 Replies

10. UNIX for Dummies Questions & Answers

Killing a stubborn process...

I have a stubborn process on my OpenBSD box that just refuses to die. It is taking up about half a meg of memory and refuses to die. It appears to be an errant gzip process that was executed from the console on 06 Jan 2002. Here is a snippet of my attempts to kill the gzip process ... (7 Replies)
Discussion started by: auswipe
7 Replies
Login or Register to Ask a Question