欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在centos7 中把oracle设置为开机自启动

程序员文章站 2022-07-13 10:29:35
...

在etc目录下创建init文件夹,在init文件夹下面创建oracle文件,文件的内容为:

 

ORA_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1
ORA_OWNER=oracle
LOGFILE=/var/log/oracle.log
echo "#################################" >> ${LOGFILE}
date +"### %T %a %D: Run Oracle" >> ${LOGFILE}
if [ ! -f ${ORA_HOME}/bin/dbstart ] || [ ! -f ${ORA_HOME}/bin/dbshut ]; then
    echo "Error: Missing the script file ${ORA_HOME}/bin/dbstart or ${ORA_HOME}/bin/dbshut!" >> ${LOGFILE}
    echo "#################################" >> ${LOGFILE}
    exit
fi
start(){
    echo "###Startup Database..."
    su - ${ORA_OWNER} -c "${ORA_HOME}/bin/dbstart ${ORA_HOME}"
    echo "###Done."
    echo "###Run database control..."
    su - ${ORA_OWNER} -c "${ORA_HOME}/bin/emctl start dbconsole"
    echo "###Done."
}
stop(){
    echo "###Stop database control..."
    su - ${ORA_OWNER} -c "${ORA_HOME}/bin/emctl stop dbconsole"
    echo "###Done."
    echo "###Shutdown Database..."
    su - ${ORA_OWNER} -c "${ORA_HOME}/bin/dbshut ${ORA_HOME}"
    echo "###Done."
}
case "$1" in
    'start')
        start >> ${LOGFILE}
    ;;
    'stop')
        stop >> ${LOGFILE}
    ;;
    'restart')
        stop >> ${LOGFILE}
        start >> ${LOGFILE}
    ;;
esac
date +"### %T %a %D: Finished." >> ${LOGFILE}
echo "#################################" >> ${LOGFILE}
echo ""

 上面的代码中ORA_HOME 为oracle的安装目录

 

 

然后执行命令

 

chmod a+x /etc/init/oracle

 使文件oracle有执行的权限

 

 

启动oracle服务用命令

/etc/init/oracle  start

 停止oracle服务用命令

/etc/init/oracle stop

在centos7 下面把/etc/init/oracle 注册为系统的服务

在/usr/lib/systemd/system目录下创建文件orcle.service,文件的内容如下:

[Unit]
Description=oracle auto start regist service
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/etc/init/oracle start 
ExecStop=/etc/init/oracle stop

[Install]
WantedBy=multi-user.target

 修改文件的权限

chmod 754 /usr/lib/systemd/system/oracle.service

 在任意目录下执行

systemctl enable oracle.service

 会有提示信息:

Created symlink from /etc/systemd/system/multi-user.target.wants/oracle.service to /usr/lib/systemd/system/oracle.service.

 附:

 

systemctl命令

 

启动服务

systemctl start oracle.service

 

设置开机自启动

systemctl enable oracle.service

 

停止开机自启动

systemctl disable oracle.service

  

查看服务当前状态

systemctl status oracle.service

  

重新启动服务

systemctl restart X.service

 

查看所有已启动的服务

systemctl list-units --type=service
systemctl list-unit-files