Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cntl+z Trap is not detecting ??? Help required to add a trap detection ???
# 1  
Old 10-08-2008
Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks,

I have tried to add some trap detection in the below script....this script is used to monitor database a rather awkward way Smilie....

The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count values and other things...if i kill the leaves a lot of mess in the folder......and also may leave many sub process hanging around.....increasing my process count.........

I have already created a function that does the mess cleaning......and for time being i am stopping the script by echoing "1" to a file named panic...which is constantly monitored by the script that invokes the above function if 1.

I would like to eliminate the above awkward procedure.....and add a trap detection such that when i press cntl+z the script should catch it and invoke the function mentioned above that cleans up the mess......

I tried to add traps for SIGTSTP in all possible locations......but this leads to either hanging up of the script or will just kill the script without executing the mess cleaning function.....Kindly help me in putting the trap command in right position inside the that it will catch the trap and executes the mess cleaning function

fin() --clean up function...
handle() ---calls up the fin

start reading from
"#check for table list" after handle()

The script does some preliminary validation and requirements before starting the actual monitoring process....which is a continuous comparison of "select count (*) ..." statement results and diff files .....for a list of tables passed by an external file named "tlist"......all this (except the preliminary validation) works under a delay by the following while loop.

"while [ !$(sleep 1) ]"

so that the result is scrolled slowly.........
If i add a trap inside this loop...the script will hang..such i need to kill it manually by opening another terminal...if i add trap at the last line of the file...the script will get killed without invoking the handle()....
I only need to know where to put a trap in this script.........kindly help.....Smilie....
# By default panic file is not deleted. So echo 0 to it.

# sentinel


#echo FS:cntRowTmp:- $cntRowTmp
#echo FS:cntRow:- $cntRow
#echo FS:table:- $table
#echo FS:sno:- $sno

if [ $cntRowTmp -ne $cntRow ]; then
#echo FS:not equal.
if [ $cntRowTmp -lt $cntRow ]; then
#echo FS:less than.
echo $serNo >> dbLog.bas
cecho blue "Sentinel [$table] reporting: row(-)."
echo "[$table]: row(-)." >> dbLog.bas
#echo $cntRowTmp > $table"Cnt.bas"
mv $table"Tmp.bas" $table"Cnt.bas"

if [ $cntRowTmp -gt $cntRow ]; then
#echo FS:greater than.
echo $serNo >> dbLog.bas
cecho green "Sentinel [$table] reporting: row(+)."
echo "[$table]: row(+)." >> dbLog.bas
#echo $cntRowTmp > $table"Cnt.bas"
mv $table"Tmp.bas" $table"Cnt.bas"


if [ $cntRowTmp -eq $cntRow ]; then
#echo FS:equal.
diff $x"Ini.bas" $x"Fin.bas" > /dev/null
exitStatus=`echo $?`
# if [ "$((`diff $x"Ini.bas" $x"Fin.bas" | wc -l`))" != "0" ]; then
if [ $exitStatus -eq 1 ]; then
#echo FS:col mod.
echo $serNo >> dbLog.bas
cecho grey "Sentinel [$table] reporting: row(mod)."
echo "[$table]: row(mod)." >> dbLog.bas
echo $cntRowTmp > $table"Cnt.bas"
mv $x"Fin.bas" $x"Ini.bas"



# exit
cecho blue "BASIL B.C. 2008"
cecho bold "Bye."
exit 127
# clean up
pstty=`tty | cut -c10-11 | tr -d ' '`
tmStmp=`date | (read u v w x y z; echo $w$x) | sed '/:/s///g'`
echo "Dumping individual table snapshots to dbSS$tmStmp.bas."
cat *Snp.bas > "dbSS"$tmStmp".bas"
echo "Cleaning temporary files."

rm -f *Fin.bas
rm -f *Snp.bas
rm -f *Ini.bas
rm -f *Cnt.bas
rm -f *Tmp.bas

killmp db2bp $pstty
killmp db2 $pstty
killmp xargs $pstty
killmp sleep $pstty

cecho red "Panic mode."
# check for table list
if [ $((`ls tlist.bas |wc -l`)) -eq 0 ]; then
cecho red "Table list (tlist.bas) is missing."
# check for arguments
if [ $# -ne 2 ]; then
cecho red >&2 "Usage: dbmon <database name> <time to live>"

# check for panic file
if [ $((`ls panic |wc -l`)) -eq 0 ]; then
echo "Setting up panic status..."
echo "0" > panic
# connecting to db2
echo "Trying to connect to the database $1 ..."
db2 connect to $1 > dbCTmp.bas

while read line; do
cecho red "$line" |xargs |grep "42705" && echo "Database not found."
done < dbCTmp.bas

if [ $exitCode -ne 0 ]; then
# check fo invalid ttl
if [[ $2 =~ ^[a-zA-Z]+$ ]]; then
cecho red "Invalid TTL. (TTL has be a number.)"

if [ $2 -lt 1 ]; then
cecho red "TTL underflow. (Raise the time to live by 1 or above)."

# panic flag
pflag=`cat panic`

refNo=`date | (read u v w x y z; echo $w$x) | sed '/:/s///g'`
# collecting tables
tableList=`cat tlist.bas`

# monitor
while [ "$flag" == 0 ]
echo "Activating Sentinels with a life of $2 ..."
if [ $pflag -ne 1 ]; then

for x in $tableList; do
cecho purple "$bCnt.Creating Sentinel base for table $x..."

db2 "select * from $x" > $x"Ini.bas"
touch $x"Fin.bas"

echo $flag > $x"Cnt.bas"
bCnt=$(($bCnt + 1))
cecho red "Base under previous panic. Restart again."
echo "0" > panic

while [ !$(sleep 1) ]
for x in $tableList; do
echo "Reading $x." >> dbLog.bas
db2 "select count(*) from $x" | xargs | linmon > $x"Tmp.bas"

db2 "select * from $x" > $x"Fin.bas"
echo "$x row snap shot for Opr:$refNo" >> $x"Snp.bas"
date >> $x"Snp.bas"
diff $x"Ini.bas" $x"Fin.bas" >> $x"Snp.bas"

cntRowTmp=`cat $x"Tmp.bas"`
cntRow=`cat $x"Cnt.bas"`
refNo=$(($refNo + 1))

#echo ":cntRowTmp-"$cntRowTmp
#echo ":cntRow-"$cntRow

sentinelRow $cntRowTmp $cntRow $x $refNo

# for time being manually fill 1 this file for panic.
if [ "$pflag" = "1" ]; then

if [ $ttl -eq 1 ]; then

ttl=$(($ttl - 1))
cecho bold "Life Remaining:"$ttl

# 2  
Old 10-08-2008
I can't find any trap command in your script. Add this command after the fin function to trap ^C :

trap fin SIGINT

Try other signals (man 7 signal).

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting


Hi Ppl, Need help $ cat #!/bin/bash trap cleanup 1 2 3 15 cleanup() { echo “I was running \”$BASH_COMMAND\” when you interrupted me.” echo “Quitting.” exit 1 } while : do echo -en “hello. “ sleep 1 (3 Replies)
Discussion started by: heman96
3 Replies

2. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

3. Shell Programming and Scripting

what does this 'trap' do?

trap "" 1 2 3 Thanks, -dog (1 Reply)
Discussion started by: landog
1 Replies

4. Shell Programming and Scripting


Hi At the beginning of my script, i will create a file and at the end of the script i will delete that. But i got to delete the file even if the process is forcefully killed, or server is rebooted... I think i can make use of trap signal, but couldnt figure out how and where to use in my... (4 Replies)
Discussion started by: vasuarjula
4 Replies

5. Shell Programming and Scripting

How to trap

I have a script #!/bin/ksh trap cleanup 20 cleanup() { cat $t.log echo Caught exit 1 } if ;then echo Found >>t.log exit 20 else echo Not found >>t.log exit 20 fi (5 Replies)
Discussion started by: thana
5 Replies

6. Shell Programming and Scripting


Hi All "Identify the behavior of traps, mechanism to implement traps in the snmp framework" What does it mean?? Can anybody explain.. Whats this Trap?? Thanx in Advance. (1 Reply)
Discussion started by: jeenat
1 Replies

7. UNIX for Advanced & Expert Users

Need help with trap

Hi Our problem is knowing: What is the "best" way of simulating a TRAP for ERR within a function, since we know this will not work directly with ksh93 and aix5. How can we save the error encountered in the function and then deal with it in the calling script? Thanks! (3 Replies)
Discussion started by: theteeth07
3 Replies

8. Shell Programming and Scripting

Building a better mouse trap, or How many lines of code does it take to trap a mouse?

Hello all, I'm hoping to get a little insight from some of the wily veterans amongst you. I've written a script to check for new outgoing files to our vendors located on our ssl server. It seems to be working ok, but the final question here, will be one of logic, and/or a better way to... (4 Replies)
Discussion started by: mph
4 Replies

9. Shell Programming and Scripting


I'd like to use "trap" command on my unix machine sunOS 5.7. But somehow when I do "which trap" command, it's no where to be found. Any one knows how I can get it installed? Thanks!! (9 Replies)
Discussion started by: whatisthis
9 Replies

10. UNIX for Advanced & Expert Users

Need help with trap

My problem is this: I need to have a catch-all for my processes. An example would be, using a trap, in the parent, to catch any non-0 exit or invalid command (a way to catch core dumps would be cool, too) in not only the parent, but it's children and they're children. Not only that, but I also... (7 Replies)
Discussion started by: marc6057
7 Replies
Login or Register to Ask a Question

Featured Tech Videos