Create an init folder in the etc directory, and create an oracle file under the init folder. The contents of the file are:
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 be 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 in the above code is the installation directory of oracle
then execute the command
chmod a+x /etc/init/oracle
Make the file oracle have execute permission
Start the oracle service with the command
/etc/init/oracle start
Stop the oracle service with the command
/etc/init/oracle stop
Register /etc/init/oracle as a system service under centos7
Create the file orcle.service in the /usr/lib/systemd/system directory with the following contents:
[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
Modify file permissions
chmod 754 /usr/lib/systemd/system/oracle.service
Execute in any directory
systemctl enable oracle.service
There will be prompt information:
Created symlink from /etc/systemd/system/multi-user.target.wants/oracle.service to /usr/lib/systemd/system/oracle.service.
Attached:
systemctl command
start the service
systemctl start oracle.service
Set up to start automatically
systemctl enable oracle.service
stop auto-start
systemctl disable oracle.service
View the current status of the service
systemctl status oracle.service
restart the service
systemctl restart X.service
View all started services
systemctl list-units --type=service systemctl list-unit-files