Oracle自动启动脚本配置 - Linux 6/7

需要为Oracle数据库配置自动启动么?这本身就是一个需要考虑的事情^_^.

下面提供两个脚本,分别用来配置Linux 6和7的自动启动服务。

适用环境:Red Hat/CentOS/OEL 6/7

For 6:

#!/bin/bash

echo "获取Oracle Home路径"
ORACLE_HOME=$(cat /etc/oratab | grep ^[a-zA-Z] | awk -F":" '{print $2}' | uniq | head -1)
echo "Oracle Home: $ORACLE_HOME"
echo -n "请确认Oracle Home是否正确(Y/n): "
read confirm
if [ "x$confirm" = "x" ] || [ "x$confirm" = "xY" ] || [ "x$confirm" = "xy" ]; then ls -ld $ORACLE_HOME; else exit; fi

echo "设置服务自动启动"
cat > /etc/init.d/oradb <<!
#!/bin/bash
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_HOME to be equivalent to the $ORACLE_HOME
# from which you wish to execute dbstart and dbshut;
#
# Set ORA_OWNER to the user id of the owner of the
# Oracle database in ORA_HOME.
#

ORA_OWNER=oracle
ORA_HOME=$ORACLE_HOME

if [ ! -f \$ORA_HOME/bin/dbstart ]
then
echo "Oracle: cannot find dbstart command!"
exit
fi

RETVAL=0
# See how we were called.
case "\$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - \$ORA_OWNER -c "\$ORA_HOME/bin/dbstart \$ORA_HOME"
RETVAL=\$?
touch /var/lock/subsys/oradb
;;
'stop')
# Stop the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - \$ORA_OWNER -c "\$ORA_HOME/bin/dbshut \$ORA_HOME"
RETVAL=\$?
rm -f /var/lock/subsys/oradb
;;
'status')
oraProcess=\$(ps -ef | grep ora_pmon | wc -l)
if [ \$oraProcess -eq 0 ]; then
echo "Oracle Service is not running!"
else
echo "Oracle Running Processes: "
ps -ef | grep ora_ | grep -v grep | awk '{print " ├─"\$2" "\$8}'
fi
RETVAL=\$?
;;
*)
echo "usage: \$0 {start|stop|status}"
exit
;;
esac

exit \$RETVAL
!

chmod 750 /etc/init.d/oradb
chkconfig --add oradb

echo "打开自动启动设置 - /etc/oratab"
sed -i '/^[a-zA-Z]/{s/N/Y/}' /etc/oratab
grep ^[a-zA-Z] /etc/oratab | grep Y


echo -e "服务启动命令:
service oradb start \n
service oradb status \n
service oradb stop
"

For 7:

#!/bin/bash

echo "获取Oracle Home路径"
ORACLE_HOME=$(cat /etc/oratab | grep ^[a-zA-Z] | awk -F":" '{print $2}' | uniq | head -1)
echo "Oracle Home: $ORACLE_HOME"
echo -n "请确认Oracle Home是否正确(Y/n): "
read confirm
if [ "x$confirm" = "x" ] || [ "x$confirm" = "xY" ] || [ "x$confirm" = "xy" ]; then ls -ld $ORACLE_HOME; else exit; fi

echo "安装服务"
cat > /usr/lib/systemd/system/oracle.service <<!
[Unit]
Description=Oracle Database as Service
After=syslog.target network.target

[Service]
User=oracle
Group=oinstall
LimitMEMLOCK=infinity
LimitNOFILE=65535
Type=oneshot
RemainAfterExit=yes
Restart=no
Environment=NLS_LANG=AMERICAN_AMERICA.AL32UTF8
Environment=LANG=en_US.UTF-8
Environment=ORACLE_HOME=$ORACLE_HOME
ExecStart=/bin/bash -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME"
ExecStop=/bin/bash -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME"

[Install]
WantedBy=multi-user.target
!

echo "设置服务自动启动"
systemctl daemon-reload
systemctl enable oracle.service

echo "打开自动启动设置 - /etc/oratab"
sed -i '/^[a-zA-Z]/{s/N/Y/}' /etc/oratab
grep ^[a-zA-Z] /etc/oratab | grep Y

echo -e "服务启动命令:
systemctl start oracle.service \n
systemctl status oracle.service \n
systemctl stop oracle.service
"

 

posted @ 2020-06-10 14:39  Evan苏C  阅读(248)  评论(0编辑  收藏  举报