您的当前位置:首页正文

oracle cdb模式下某pdb异常关闭后自动启动--两种实现方法及脚本

2024-11-10 来源:个人技术集锦

背景:

近期运维的一套oracle一体机上的系统采用cdb模式运行了十几个pdb,由于版本bug导致偶发性的某个pdb被异常关闭不能自动启动(已配置pdb层面的自动启动但并没有什么软用),后来总结实践出了下面这两个方法实现检测某个pdb异常后自动将异常pdb启动起来的方法:

方法1:非crontab方式

编写名为autodb.sh的shell脚本
#!/bin/bash
# user env
source ~/.bash_profile
while [ 1 ]
do
sqlplus -S / as sysdba >/home/oracle/pdb_status.log<<EOF
set heading off feedback off pagesize 0 verify off echo off 
SELECT OPEN_MODE from v\$PDBS where name='TSSYS';
exit
EOF
[ ! -f /home/oracle/pdb_status.log ]&&exit 0
db_status=`cat /home/oracle/pdb_status.log`
if [ "$db_status" == "MOUNTED" ];then
sqlplus -S / as sysdba <<EOF
set heading off feedback off pagesize 0 verify off echo off 
ALTER PLUGGABLE DATABASE ALL OPEN;
exit
EOF
fi
sleep 15
done


后台执行命令 
nohup sh autodb.sh >>autodb.log 2>&1 &

方法2:crontab 方式

#!/bin/bash
# user env
source ~/.bash_profile
sqlplus -S / as sysdba >/home/oracle/pdb_status.log<<EOF
set heading off feedback off pagesize 0 verify off echo off 
SELECT OPEN_MODE from v\$PDBS where name='TSSYS';
exit
EOF
[ ! -f /home/oracle/pdb_status.log ]&&exit 0
db_status=`cat /home/oracle/pdb_status.log`
if [ "$db_status" == "MOUNTED" ];then
sqlplus -S / as sysdba <<EOF
set heading off feedback off pagesize 0 verify off echo off 
ALTER PLUGGABLE DATABASE ALL OPEN;
exit
EOF
fi

crontab -e 添加如下内容:
* * * * * sleep 15; sh /home/oracle/autodb.sh

显示全文