Auto Start/Shutdown Oracle Database 11g R2 on Linux

Oracle Oracle 11g

Installing Oracle Database 11g R2 on a Linux Server does not provide the database the possibility to preform an automatic startup/shutdown when the system is starts or shutdown, we need to do it manually!

so here is a few steps to make it happen automatically.

The automatic startup and shutdown of the Oracle database can be achieved with the files dbstart and dbshut both provided by Oracle. These files rely on the existence of the file /etc/oratab to work.

The format of the /etc/oratab file is as follows:

SID:ORACLE_HOME:AUTO

or in my example:

ORCL:/u01/app/oracle/product/11.2.0/dbhome_1:Y

To start and stop the database when the machine comes up and goes down by modifying the startup routines for the Linux machine. This is quite easy, although I should point out here that this may change depending on which flavor of Linux (slackware, debian, redhat, etc). I will show examples which work for Oracle Linux 5.x. To modify these for your own flavor of Linux, please see your Linux documentation sets.

Note: /etc/init.d is the official location for placing start up scripts and most, but not all distros follow this convention. /etc/rc.d/init.d is where Red Hat (which Oracle Linux is based on) places startup scripts, but in order to comply with modern convention, they make /etc/initd a symlink to /etc/rc.d/init.d.

Firstly, we need to create the script which will run dbshut and dbstart in the /etc/rc.d/init.d directory. Create the following file as /etc/init.d/oracle:

Note1: The parts in red are optional where if you like to have Oracle Enterprise Manager starting/shutting down with the system.
Note2: If you copy and paste the script you may/will get errors because of the double quotation and the (-) chars,

#!/bin/sh
#
# /etc/rc.d/init.d/oracle
# Description: Starts and stops the Oracle database, listeners and Enterprise Manager
# See how we were called.
case "$1" in
start)
echo "Starting Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Starting Oracle Databases as part of system up." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Starting Oracle Listeners: "
su - oracle -c "lsnrctl start" >> /var/log/oracle
echo "Done."
echo -n "Starting Oracle Databases: "
su - oracle -c dbstart >> /var/log/oracle
echo "Done."
echo -n "Starting Oracle Enterprise Manager: "
su - oracle -c "emctl start dbconsole" >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
stop)
echo "Shutting Down Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Shutting Down Oracle Databases as part of system down." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Shutting Down Oracle Enterprise Manager: "
su - oracle -c "emctl stop dbconsole" >> /var/log/oracle
echo "Done."
echo -n "Shutting Down Oracle Listeners: "
su - oracle -c "lsnrctl stop" >> /var/log/oracle
echo "Done."
rm -f /var/lock/subsys/oracle
echo -n "Shutting Down Oracle Databases: "
su - oracle -c dbshut >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
;;
restart)
echo "Restarting Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Restarting Oracle Databases as part of system up." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Restarting Oracle Listeners: "
su - oracle -c "lsnrctl stop" >> /var/log/oracle
su - oracle -c "lsnrctl start" >> /var/log/oracle
echo "Done."
echo -n "Restarting Oracle Databases: "
su - oracle -c dbshut >> /var/log/oracle
su - oracle -c dbstart >> /var/log/oracle
echo "Done."
echo -n "Restarting Oracle Enterprise Manager: "
su - oracle -c "emctl stop dbconsole" >> /var/log/oracle
su - oracle -c "emctl start dbconsole" >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
*)
echo "Usage: oracle {start|stop|restart}"
exit 1
esac

it’s important to remember to make the script executable by simply run the command:

$ chmod +x /etc/init.d/oracle

It is worth checking that this file actually correctly stops and starts the databases for your system. Check the log file, /var/log/oracle for error messages.

Once this script is working we need to create start and kill symbolic links in the appropriate runlevel directories /etc/rc.d/rcX.d.

The following commands will ensure that the databases will come up in runlevels 2,3,4 and 5:

$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc2.d/S99oracle
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc3.d/S99oracle
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc4.d/S99oracle
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc5.d/S99oracle

To stop the databases on reboot or restart we need the following links:

$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc0.d/K01oracle # Halting
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc6.d/K01oracle # Rebooting

I use the method above and it works fine for me, no issues so far, also I have to add that I did not write the whole thing it’s based on my online resources that can be easily found by just searching…

————————————–OR——————————–2nd Method————————————————————-

1-Edit the #vi /etc/oratabfile :
orcl:/home/oracle/app/oracle/product/11.2.0/dbhome_1:Y

then

vi /etc/init.d/oracle

paste this script to it and save

#!/bin/bash
#
# oracle This shell script takes care of starting and stopping
# the Oracle subsystem (oracle).
#
# chkconfig: – 64 36
# description: Oracle database server.
# processname: oracle
# pidfile: /var/run/oracle/oracle.pid
### BEGIN INIT INFO
# Provides: oracle
# Required-Start: $local_fs $remote_fs $network $named $syslog $time
# Required-Stop: $local_fs $remote_fs $network $named $syslog $time
# Short-Description: start and stop Oracle server
# Description: Oracle database server
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

start() {
if [ -e /var/lock/subsys/oracle ];then
echo “Starting Oracle Enterprise Server”
failure
else
echo “Starting Oracle Enterprise Server”
success
echo “`date “+%m/%d/%Y %H:%M:%S”` : Starting Oracle Listeners” >> /var/log/oracle/oracle.log
su – oracle -c “lsnrctl start” >> /var/log/oracle/oracle.log
echo “`date “+%m/%d/%Y %H:%M:%S”` : Starting Oracle Databases” >> /var/log/oracle/oracle.log
su – oracle -c “dbstart” >> /var/log/oracle/oracle.log
echo “`date “+%m/%d/%Y %H:%M:%S”` : Starting Oracle Enterprise Manager” >> /var/log/oracle/oracle.log
su – oracle -c “emctl start dbconsole” >> /var/log/oracle/oracle.log
touch /var/lock/subsys/oracle
fi
}

stop() {
if [ -f /var/lock/subsys/oracle ];then
echo “Shutting Down Oracle Enterprise Server”
success
echo “`date “+%m/%d/%Y %H:%M:%S”` : Shutting Down Oracle Enterprise Manager” >> /var/log/oracle/oracle.log
su – oracle -c “emctl stop dbconsole” >> /var/log/oracle/oracle.log
echo “`date “+%m/%d/%Y %H:%M:%S”` : Shutting Down Oracle Listeners” >> /var/log/oracle/oracle.log
su – oracle -c “lsnrctl stop” >> /var/log/oracle/oracle.log
echo “`date “+%m/%d/%Y %H:%M:%S”` : Shutting Down Oracle Databases” >> /var/log/oracle/oracle.log
su – oracle -c “dbshut” >> /var/log/oracle/oracle.log
rm -f /var/lock/subsys/oracle
else
echo “Shutting Down Oracle Enterprise Server”
failure
fi
}

case “$1″ in
start)
start
;;
stop)
stop
;;
status)
if [ -e /var/lock/subsys/oracle ]; then
echo -n “Oracle Enterprise Server is Running”
success
echo
else
echo -n “Oracle Enterprise Server is Not Running”
failure
echo
fi
;;
restart)
stop
sleep 2
start
;;
*)
echo $”Usage: $0 {start|stop|restart|status}”
exit 1

esac
exit 0

then
chkconfig –add oracle
chkconfig oracle on