Automating Database Startup and Shutdown 11g R2 on Linux

By Jag - August 07, 2014
Automating Database Startup and Shutdown 11g R2 on Linux

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, please read Mike’s comment, here is the script with the correct 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

TAG:
Auto Start/Shutdown Oracle Database
shutdown oracle database sqlplus
shutdown oracle database from command line
shutdown oracle database immediate
shutdown oracle database sap
shutdown oracle database in windows
shutdown oracle database instance
shutdown oracle database without sqlplus

how to shutdown oracle database linux
  • Share:

You Might Also Like

0 comments