how to trap unix signal if the process killed/interupt occured in bash...

hey champs,

I have a process running.......i have to catch/trap the signal when the process is being interupted/killed (kill -9 pid) option......

how can i achieve the same thru my process........

let my process is a.sh and it supposed to take 13 mins to complete, but due to some problem , somebody killed that process a.sh using kill -9 <pid of a.sh>, so i want to log the message that process a.sh is killed ....

how can i achieve...

You cannot trap SIGKILL. Your only option would be to monitor it from another process.

you are right...i sudn't trap the kill, but what about other signals, how can i catch .....thru trap....in bash

i am doing following

function trap_cmd {
echo "It's wrong way to stop the process."
exit 2
}

trap 'trap_cmd' 1 2 3 4 5 6 7 8 10 12 13 14 15 20

but even though CTRL-C,Z ..... it's not going to echo me It's wrong way to stop the process. :mad:

so kindly suggest....for bash.

works in bash 2.05

Unless of course you are doing something strange like piping the output of a function, in which case there will be strange behavior becasue of the focred subshell.

hey buddies, thanx a lot....
i found the problem, the thing is TRAP is not yet installed...in my box...

i need further assistance from your end, i.e.
1 I would like to install the TRAP in my box,
>The steps, from where should i get the utility, and instruction
manual from your side........can i get it ????
2 This question is the silly one, but >how to know which version of
bash i am working ????

Kindly assist ....

One more thing my box is Linux 2.6.9

trap is part of bash.

I would be *very* surprised if you don't have it.

any idea and suggest.....:confused:

I used exactly what you posted with "#!/bin/bash" at the top and "sleep 10000" at the bottom, and it worked.

So something else that you are doing is stopping it.

Post the results from:
command -V trap
echo $BASH_VERSION

hey, Perderabo

please find the o/p of
command -V trap as trap is a shell builtin
and echo $BASH_VERSION as 3.00.15(1)-release

the same thing i did, only difference is i have not given #!/bin/bash.....
and for stopping/break i used to press CTRL-C,Z as stated earlier..but it is not showing any echo..

let me try with #!/bin/bash...

let me put my script ,

#!/bin/bash
function get_connection
{
var12="$1"
v_cnt=`sqlplus -s <username>/<pwd>@<hostname> << ENDSQL | sed -e "s/Connected\.//" -e "/^$/d"
set pagesize 0 feedback off verify off heading off echo off serveroutput on size 10000
whenever sqlerror exit 1
$var12
commit;
exit ;
ENDSQL`
var12=""
}

function trap_cmd {
echo "It's wrong way to stop the process."
exit 2
}

echo " Runnnig process id is : $$"
select_outbox="SELECT outbox_retn FROM CBF_CONFIG WHERE CS_ID='"$1"';"

get_connection "$select_outbox"

echo $v_cnt

sleep 600

trap 'trap_cmd' 1 2 3 4 5 6 7 8 9 10 12 13 14 15 20

=============================
o/p

Runnnig process id is : 22345
60
-------
-----
in between this if i am pressing CTRL-C,Z, nothing is echoing.....
am i doing any wrong way to trace ?????:confused:

Move the trap statement to just after the function definition. You are currently installing the trap handler as the very last step before the script exits. Install it as soon as possible.

thanx ...it's working......

thanx again you all guys for your beautiful co-operations.:b: