OS层面kill运行超过N天的进程

背景:

如果应用使用持久连接的话(例如连接池),这些进程的存活时间应该是比较长的。不过LOCAL=NO的进程有个现象就是,当客户端崩溃的时候(例如sql plus或toad无故死掉),oracle并不会自动释放他们之间建立的监听进程,这种情况长期累积之后也会对系统造成一定的资源浪费,所以应该经常检查一下是否有这样的进程存在并kill掉它们。

面脚本是kill 掉连接时间超过7天的网络连接的进程。 把脚本放在crontab里,定时执行即可。

1、Method A 
ps -e -o pid -o etime -o args|grep LOCAL=NO|grep -v grep| \
awk '{ if (int(substr($2,1,index($2,"-")-1))>7) {print $1;} }'| \
xargs -t kill -9

2、Method B 
ps -e -o pid -o etime -o args|grep LOCAL=NO|grep -v grep| \
awk '{ split($2,arr,"-"); if (arr[1]>7) {print $1;} }'| \
xargs -t kill -9

   注:etime   ELAPSED   elapsed time since the process was started, in the form [[dd-]hh:]mm:ss.
3、Method C (not clever)
kill.sh:
 ps -e -o pid -o etime -o args|grep LOCAL=NO>/tmp/tmpfile
 cat /tmp/tmpfile|while read LINE
 do
 TIME=`echo $LINE|awk '{print $2}'`
 TIME=`echo $TIME|awk -F: '{print $1}'`
 if [ $TIME -gt 7]
 then
 echo $LINE >> /tmp/tmpflie2
 fi
 done
 cut -c 1-5 /tmp/tmpfile2 |xargs -t -n1 kill -9
 rm -f /tmp/tmpfile
 rm -f /tmp/tmpfile2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值