jillme
作者jillme课题专家组·2011-05-07 21:23
CIO·某大型银行

将INFORMIX里连接超过半个小时里没有动作的进程KILL掉

字数 3231阅读 2847评论 0赞 0
 informix 经常因为连接数的限制和其他的原因,一些用户长期占用资源,但是不操作;而其他的用户则连接不上,导致资源分配的不合理。以下脚本将连接超过30分钟,而没有操作的进程kill掉,释放出资源。
以下是脚本的内容:
#!/bin/ksh
#设置INFORMIX环境信息
INFORMIXSERVER=on_shm01;export INFORMIXSERVER ONCONFIG=onconfig.on_01;export ONCONFIG DB_LOCALE=zh_tw.big5;export DB_LOCALE SERVER_LOCALE=zh_tw.big5;export SERVER_LOCALE CLIENT_LOCALE=zh_tw.big5;export CLIENT_LOCALE LANG=zh_tw.big5;export LANG

echo "INFORMIXSERVER=$INFORMIXSERVER"
echo "ONCONFIG=$ONCONFIG"

INFORMIXDIR=/u1/informix;export INFORMIXDIR PATH=$INFORMIXDIR/bin:$PATH;export PATH DBDATE=Y4MD/;export DBDATE DBCENTURY=C;export DBCENTURY DBDELIMITER='^A';export DBDELIMITER

#检查参数
if [ $# -ne 1 ] ; then
echo "Usage: killidle.sh idletime "
echo "Ex: killidle.sh 1800 "
exit
fi

idle=$1
now=`date | awk '{ print $4 }'`
kill_time=`date '+%y/%m/%d-%H:%M:%S'`
echo "kill_time:$kill_time"
tsNow=0
hhn=`echo $now | cut -d':' -f 1`
mmn=`echo $now | cut -d':' -f 2`
ssn=`echo $now | cut -d':' -f 3`
tsNow=`expr $hhn * 3600 + $mmn * 60 + $ssn`

ToSec() {
# $1 : opentime
hh1=`echo $1 | cut -d':' -f 1`
mm1=`echo $1 | cut -d':' -f 2`
ss1=`echo $1 | cut -d':' -f 3`
ts1=`expr $hh1 * 3600 + $mm1 * 60 + $ss1 ` 
# $2 : writetime 
hh2=`echo $2 | cut -d':' -f 1` 
mm2=`echo $2 | cut -d':' -f 2` 
ss2=`echo $2 | cut -d':' -f 3` 
ts2=`expr $hh2 * 3600 + $mm2 * 60 + $ss2 ` if [ ts1 -gt ts2 ] ;
then
ts=`expr $tsNow - $ts1`
else
ts=`expr $tsNow - $ts2`
fi
#echo $ts
}

i=0
cnt=1
sesID=""
opnetime=""
readtime=""
writetime=""

#获取informix sesion
onstat -g ses > t1.tmp
tail +6 t1.tmp>ifx_u.tmp
rm t1.tmp
ps -ef|awk '{print $1 $2}' > ps_ef.tmp

#获取每个session的开始连接和最后1次读写时间
for f in `onstat -g ntt | grep sqlexec | awk '{ print $3,$4,$5,$6 }'` do
i=`expr $i + 1`
case $i in
1) sesID=$f
;;
2) opentime=$f
;;
3) readtime=$f
;;
4) writetime=$f
ToSec $readtime $writetime
# echo "Session ID:$sesID , opentime:$opentime , readtime:$readtime , writetime:$writetime , idle time:$ts Sec. "
if [ $ts -gt $idle ] ; # 比较 idle time
then
echo "$cnt)SeID:$sesID,OTime:$opentime,RTime:$readtime,WTime:$writetime,IdleTime:$ts Sec."
while read L1
do
t_ses=`echo $L1 | awk '{ print $1 }'`
# echo "$L1,t_ses is: $t_ses -sesID is :$sesID"
if [ $t_ses -eq $sesID ] ;
then
t_uname=`echo $L1 | awk '{ print $2 }'` # session woner
t_kill=`grep $t_uname $INFORMIXDIR/top_nokill.list | grep -v grep | wc | awk '{ print $1 }'`
if [ t_kill -eq 0 ]; # pGb top_nokill.list,NnR
then
t_pid=`echo $L1 | awk '{ print $4 }'`
## t_pid_no=`ps -ef | grep $t_pid | grep -v grep | wc | awk '{ print $1 }'`
t_pid_no=`grep $t_pid ps_ef.tmp|wc -l`
t_tty=`echo $L1 | awk '{ print "pts/",$3 }' | tr -d ' '`
t_host=`echo $L1 | awk '{ print $5 }'`
kill_time=`date '+%H:%M:%S'`
if [ $t_pid_no -eq 1 ] ;
then
echo "Kill:PID:$t_pid,Uname:$t_uname,Tty:$t_tty,pid_no:$t_pid_no,time:$kill_time"

onmode -z $t_ses
kill -9 $t_pid
else
echo "Keep(1):PID:$t_pid ,Uname:$t_uname ,tty:$t_tty,pid_no:$t_pid_no"

fi
else
echo "Keep(2):PID:$t_pid ,Uname:$t_uname ,tty:$t_tty"
fi
break;
fi
done < ifx_u.tmp
cnt=`expr $cnt + 1`
fi
i=0;;
esac
done
kill_time=`date '+%y/%m/%d-%H:%M:%S'`
echo "kill_time:$kill_time"
rm ifx_u.tmp
rm ps_ef.tmp
-------------------------

将此脚本放入crontab中 设置成每30分钟执行次。
但是一定是root用户,因为其他的用户无法kill非自己的用户的进程

# crontab -l
0,30 8,9,10,11,12,13,14,15,16,17 * * * sh /u1/informix/killidle.sh 1800 >>killidle.log 2>&1

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广