Hi folks,
I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way
....
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 script...it 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 script.......so 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.....
....
-------------------------------------------------------------------------
# WARNING: THIS SCRIPT IS UNDER TESTING.
# By default panic file is not deleted. So echo 0 to it.
# sentinel
sentinelRow(){
cntRowTmp=$1
cntRow=$2
table=$3
serNo=$4
#debug
#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"
fi
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"
fi
fi
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
fi
echo $cntRowTmp > $table"Cnt.bas"
mv $x"Fin.bas" $x"Ini.bas"
fi
}
#---------------------------------------------------------------------------79
# exit
getOut(){
cecho blue "BASIL B.C. 2008"
cecho bold "Bye."
exit 127
}
# clean up
fin(){
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
getOut
}
handle(){
cecho red "Panic mode."
fin
}
# check for table list
if [ $((`ls tlist.bas |wc -l`)) -eq 0 ]; then
cls
cecho red "Table list (tlist.bas) is missing."
getOut
fi
# check for arguments
if [ $# -ne 2 ]; then
cecho red >&2 "Usage: dbmon <database name> <time to live>"
getOut
fi
# check for panic file
if [ $((`ls panic |wc -l`)) -eq 0 ]; then
cls
echo "Setting up panic status..."
echo "0" > panic
fi
# connecting to db2
echo "Trying to connect to the database $1 ..."
db2 connect to $1 > dbCTmp.bas
exitCode=$?
while read line; do
cecho red "$line" |xargs |grep "42705" && echo "Database not found."
done < dbCTmp.bas
if [ $exitCode -ne 0 ]; then
getOut
fi
# check fo invalid ttl
if [[ $2 =~ ^[a-zA-Z]+$ ]]; then
cecho red "Invalid TTL. (TTL has be a number.)"
getOut
fi
if [ $2 -lt 1 ]; then
cecho red "TTL underflow. (Raise the time to live by 1 or above)."
getOut
fi
# panic flag
pflag=1
panicFlag(){
pflag=`cat panic`
}
flag=0
refNo=`date | (read u v w x y z; echo $w$x) | sed '/:/s///g'`
ttl=$2
# collecting tables
tableList=`cat tlist.bas`
panicFlag
#---------------------------------------------------------------------------79
# monitor
while [ "$flag" == 0 ]
do
echo "Activating Sentinels with a life of $2 ..."
bCnt=1
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))
done
else
cecho red "Base under previous panic. Restart again."
echo "0" > panic
handle
fi
while [ !$(sleep 1) ]
do
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))
#debug
#echo ":cntRowTmp-"$cntRowTmp
#echo ":cntRow-"$cntRow
sentinelRow $cntRowTmp $cntRow $x $refNo
panicFlag
# for time being manually fill 1 this file for panic.
if [ "$pflag" = "1" ]; then
handle
fi
done
if [ $ttl -eq 1 ]; then
fin
fi
ttl=$(($ttl - 1))
cecho bold "Life Remaining:"$ttl
done
flag=1
done