Wednesday, December 29, 2010

11g R2 (11.2.0.2) -- Script to Auto Start/Shutdown databases during reboots.

I have a standalone 11 R2 (11.2.0.2) non-ASM database. The following is my script which can auto start/stop database and listener during system reboots. Please note that script reads /etc/oratab file to check which database to start. During startups, any database entry which has the "Y" at the end in the /etc/oratab file, will be started and anything which ends with "N", will be skipped.

Step 1:
Login as root and create a file called /etc/init.d/oracle. Add the following lines in the script.

#!/bin/bash

#######################################################################
#
# Run-level Startup script for the Oracle Instance and Listener
#
# chkconfig: 345 98 34
# description: Startup/Shutdown script for 11g Oracle instances
#
#######################################################################

#######################################################################
#
# Note:
# cp $ORACLE_HOME/bin/dbstart $ORACLE_HOME/bin/dbstart.orig
# Make the following changes in $ORACLE_HOME/bin/dbstart
#
# Line#80 has the following:
# ORACLE_HOME_LISTNER=$1
#
# Replace it with the following two lines:
# export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
# ORACLE_HOME_LISTNER=$ORACLE_HOME
#
# cp $ORACLE_HOME/bin/dbshut $ORACLE_HOME/bin/dbshut.orig
# Make the following changes in $ORACLE_HOME/bin/dbshut
#
# Line#50 has the following:
# ORACLE_HOME_LISTNER=$1
#
# Replace it with the following two lines:
# export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
# ORACLE_HOME_LISTNER=$ORACLE_HOME
#
# save the file and proceed with the steps below
#
#######################################################################

#######################################################################
#Instructions to start start isntances manually
#
# To Start --> /etc/init.d/oracle start
# To Stop --> /etc/init.d/oracle stop
# To restrart --> /etc/init.d/oracle restart
#
#######################################################################

ORA_HM="/u01/app/oracle/product/11.2.0/db_1"
ORA_OWNR="oracle"
# if the executables do not exist -- display error
if [ ! -f $ORA_HM/bin/dbstart -o ! -d $ORA_HM ]
then
echo "Oracle startup: cannot start"
exit 1
fi

# depending on parameter -- startup, shutdown, restart
# of the instance and listener or usage display

case "$1" in
start)
# starting Oracle instances
echo -n "starting Oracle Instances"
su - $ORA_OWNR -c $ORA_HM/bin/dbstart
touch /var/lock/subsys/oracle
ps -ef |grep pmon |grep -v grep
ps -ef |grep lsnr |grep -v grep
echo "OK"
;;
stop)

# Oracle listener and instance shutdown
su - $ORA_OWNR -c $ORA_HM/bin/dbshut
rm -f /var/lock/subsys/oracle
ps -ef |grep pmon |grep -v grep
ps -ef |grep lsnr |grep -v grep
echo "OK"
;;

reload|restart)
$0 stop
$0 start
ps -ef |grep pmon |grep -v grep
ps -ef |grep lsnr |grep -v grep
echo "OK"
;;
*)
echo "Usage: $0 start|stop|restart|reload"
exit 1
esac
exit 0
clear
###################### END of SCRIPT ##############################


Step-2:
Change the permission of the script
chmod 750 /etc/init.d/oracle

Step-3:
Execute the following so that runlevel 3,4 & 5 will execute the script during system reboots.
chkconfig --level 345 oracle on

Step-4
Execute the script and verify everything is working as expected. Alternatively, you can download the script directory from here.


Additionally you can turn on and turn on with the following.
[root@bl-mm-db-dev ~]# /sbin/chkconfig oracle off
[root@bl-mm-db-dev ~]# /sbin/chkconfig --levels 345 oracle on

That's it. We are done.


--Moid

No comments:

Post a Comment

Followers