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.

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,

how about this:

. ./scriptb.sh

Wow thats a great idea.... but what I was really hoping to do eventually was

instead of

.
.
./scriptb.sh
.
.

I will be doign

.
.
rsh machineb "/scriptb.sh"
.
.

I guess this one isn't so easy to source or kill as its being run remotely?

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:

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

trap 'killem_all' INT TERM EXIT

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":

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

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

(
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":

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.

wow that looks good, I think the idea of using & and wait was excellent to get around the blocking.

Thanks.