ITPub博客

首页 > 数据库 > Oracle > oracle 11g自动杀锁脚本

oracle 11g自动杀锁脚本

原创 Oracle 作者:水逸冰 时间:2018-02-14 12:14:13 0 删除 编辑
最近临近春节,客户的一套11gRAC下午经常会出现锁,开发人员都回家了无法修改逻辑。为了避免过年期间因为这个每天都播vpn处理,所以赶忙写了一个自动杀锁的脚本。
这个脚本有几点需要注意的:
1.针对于linux系统
2.oracle 11g单实例和RAC都可以
3.考虑到了active的锁不能杀
4.有的锁的锁源会是一些系统进程,担心杀掉以后库会直接宕掉,脚本也考虑到了这一点。
5.客户这台服务器上有两套RAC,为了安全起见,sqlplus前我都export了一下环境变量。

可以把这这个脚本发给值班室的兄弟们,出现了锁跑一下,也可以写到crontab里面。

祝大家春节愉快!


#!/bin/bash

#get lock infomation
export ORACLE_SID=ogg1
sqlplus -s "/as sysdba" <<eof
spool /home/oracle/killholder_tmp.sql
select 'alter system kill session ''' || s.SID || ',' || s.SERIAL# || ',@' ||
       s.INST_ID || ''' immediate;' killsql
  from gv\$lock l, gv\$session s
where (id1, id2, l.type) in
       (select id1, id2, type from gv\$lock where request > 0)
   and l.sid = s.sid
   and l.inst_id = s.inst_id
   and s.status='INACTIVE'
   and s.type='USER'
   and decode(request, 0, 'Holder:', ' Waiter:') = 'Holder:'
order by id1, ctime desc, request;
spool off
exit
eof

#kill the lock holder
more /home/oracle/killholder_tmp.sql |grep '^a' > /home/oracle/killholder.sql
export ORACLE_SID=ogg1
sqlplus -s "/as sysdba" <<eof
@/home/oracle/killholder.sql
exit
eof

#check lock again
export ORACLE_SID=ogg1

sqlplus -s "/as sysdba" <<eof
spool /home/oracle/event.txt
select inst_id, event#, event,count(*) from gv\$session
where wait_class# <> 6
group by inst_id, event#,event
order by 1,4 desc;
spool off
exit
eof
TXLOCKNUM=$(more /home/oracle/event.txt|grep 'TX'|wc -l)
TMLOCKNUM=$(more /home/oracle/event.txt|grep 'TM'|wc -l)
if [ $TXLOCKNUM -eq 0 ] && [ $TMLOCKNUM -eq 0  ]
then
        echo "there is no lock!"
else
        echo "there is still having lock! "
fi

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31480688/viewspace-2151089/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
精通oracle,mysql和linux,热衷于研究数据库,擅长shell和Python自动化运维。VX:18302174682

注册时间:2017-08-05

  • 博文量
    117
  • 访问量
    188646