将该脚本放到oracle用户下面,直接运行./kill_conn.sh,脚本会自动搜索数据库中是否有表被锁,如果发现有表被锁,则进行解锁操作,否则退出:
kill_conn.sh:
#!/bin/sh
#######################################################################
#Function: unlocked the table of database
#Author: fgx
#Data: 20091229
#######################################################################
if [ -s temp.sql ];then
touch temp.sql
fi
sqlplus "/as sysdba" <<SQL >/dev/null 2>&1
spool ./temp.sql;
select t2.sid,t2.serial# from v/$locked_object t1,v/$session t2 where t1.session_id=t2.sid;
select count('*') from v/$locked_object;
spool off;
SQL
ser=`grep -n COUNT temp.sql|awk -F : '{print $1p}'`
ser=`expr $ser + 2`
total_all=`sed -n ${ser}p temp.sql`
if [ $total_all -eq 0 ];then
echo "The database has not been locked !"
exit 0
fi
num=`grep -n "SID" temp.sql|awk -F : '{print $1p}'`
num=`expr $num + 2`
i=0
touch temp1.sql
while [ $i -lt ${total_all} ]
do
sid=`sed -n ${num}p temp.sql|awk '{print $1p}'`
ser=`sed -n ${num}p temp.sql|awk '{print $2p}'`
echo alter system kill session /'${sid},${ser}/'/; >>temp1.sql
i=`expr $i + 1`
num=`expr $num + 1`
done
sqlplus "/as sysdba" <<END >/dev/null 2>&1
@temp1.sql;
;
END
echo "Unlocked the table of the database success!"
rm -rf temp.sql temp1.sql