If you are using Oracle Clusterware 10gR2 or above for RAC or just for a single instance using ASM, the Clusterware automatically starts and stops the Oracle database instances and listeners, so the following procedures are not necessary. Where the Clusterware is not being used, these methods allow you to automate the startup and shutdown of databases on Linux.
The "su" Command
The following represents the Oracle recommended method for automating database startup and shutdown of Oracle 9i instances on Linux, but it works equally well for Oracle 10g, 11G and 12c also. It can be used on any RHEL-style distribution, including Oracle Linux, up to an including RHEL7.
Once the instance is created, edit the "/etc/oratab" file setting the restart flag for each instance to 'Y'.
PROD:/u01/app/oracle/product/11.2.0/PROD:Y
Create a file called "/etc/init.d/oracle" as the root user, containing the following code.
#!/bin/sh
#
# /etc/rc.d/init.d/dbase
# Description: Starts and stops the Oracle database and listeners
# See how we were called.
ORAHOME1=/u01/app/oracle/product/11.2.0/db_1
ORAHOME2=/u01/app/oracle/product/11.2.0/PROD
case "$1" in
start)
echo -n "Starting All Oracle Databases: "
echo "----------------------------------------------------" >> /var/log/dbase.log
date +"! %T %a %D : Starting Oracle Databases as part of system up." >> /var/log/dbase.log
echo "----------------------------------------------------" >> /var/log/dbase.log
echo -n "Starting Oracle Listeners:PROD"
su - oracle -c "$ORAHOME2/bin/lsnrctl start PROD" >> /var/log/dbase.log
echo "Done."
su - oracle -c dbstart >> /var/log/dbase.log
echo "Done."
echo -n "Starting Oracle Listeners:ORCL "
su - oracle -c "$ORAHOME1/bin/lsnrctl start ORCL" >> /var/log/dbase.log
echo "Done."
echo ""
echo "----------------------------------------------------" >> /var/log/dbase.log
date +"! %T %a %D : Finished." >> /var/log/dbase.log
echo "----------------------------------------------------" >> /var/log/dbase.log
echo ""
echo "----------------------------------------------------" >> /var/log/dbase.log
date +"! %T %a %D : Finished." >> /var/log/dbase.log
echo "----------------------------------------------------" >> /var/log/dbase.log
touch /var/lock/subsys/oracle
;;
stop)
echo -n "Shutting Down Oracle Listeners: "
echo "----------------------------------------------------" >> /var/log/dbase.log
date +"! %T %a %D : Shutting Down Oracle Databases because of system down." >> /var/log/dbase.log
echo "----------------------------------------------------" >> /var/log/dbase.log
su - oracle -c "$ORAHOME1/bin/lsnrctl stop ORCL" >> /var/log/dbase.log
echo "Done. ORCL"
su - oracle -c "$ORAHOME2/bin/lsnrctl stop PROD" >> /var/log/dbase.log
echo "Done. PROD"
rm -f /var/lock/subsys/oracle
echo -n "Shutting Down All Oracle Databases: "
su - oracle -c dbshut >> /var/log/dbase.log
echo "Done."
echo ""
echo "----------------------------------------------------" >> /var/log/dbase.log
date +"! %T %a %D : Finished." >> /var/log/dbase.log
echo "----------------------------------------------------" >> /var/log/dbase.log
;;
restart)
echo -n "Restarting Oracle Databases: "
echo "----------------------------------------------------" >> /var/log/dbase.log
date +"! %T %a %D : Restarting Oracle Databases as part of system up." >> /var/log/dbase.log
echo "----------------------------------------------------" >> /var/log/dbase.log
su - oracle -c dbshut >> /var/log/dbase.log
su - oracle -c dbstart >> /var/log/dbase.log
echo "Done."
echo -n "Restarting Oracle Listeners: ORCL"
su - oracle -c "$ORAHOME1/bin/lsnrctl stop ORCL" >> /var/log/dbase.log
su - oracle -c "$ORAHOME1/bin/lsnrctl start ORCL" >> /var/log/dbase.log
echo "Done. ORCL"
echo -n "Restarting Oracle Listeners: PROD"
su - oracle -c "$ORAHOME2/bin/lsnrctl stop PROD" >> /var/log/dbase.log
su - oracle -c "$ORAHOME2/bin/lsnrctl start PROD" >> /var/log/dbase.log
echo "Done. PROD"
echo ""
echo "----------------------------------------------------" >> /var/log/dbase.log
date +"! %T %a %D : Finished." >> /var/log/dbase.log
echo "----------------------------------------------------" >> /var/log/dbase.log
touch /var/lock/subsys/oracle
;;
*)
echo "Usage: oracle {start|stop|restart}"
exit 1
esac
Use the chmod
command to set the privileges to 750.
chmod 750 /etc/init.d/oracle
Associate the "oracle" service with the appropriate run levels and set it to auto-start using the following command.
chkconfig --add oracle
For Multiple database please follow the steps. Edit the dbstart file on your first ORACLE_HOME as per the /etc/oratab and add the bellow lines to add the multiple listeners.
cd $ORACLE_HOME/bin
vim dbstart (Edit the file and add the oracle home and listener name)
#PROD
ORACLE_HOME_LISTNER=/u01/app/oracle/product/11.2.0/PROD
if [ ! $ORACLE_HOME_LISTNER ] ; then
echo "ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener"
echo "Usage: $0 ORACLE_HOME"
else
LOG=$ORACLE_HOME_LISTNER/listener.log
# Set the ORACLE_HOME for the Oracle Net Listener, it gets reset to
# a different ORACLE_HOME for each entry in the oratab.
export ORACLE_HOME=$ORACLE_HOME_LISTNER
# Start Oracle Net Listener
if [ -x $ORACLE_HOME_LISTNER/bin/tnslsnr ] ; then
echo "$0: Starting Oracle Net Listener" >> $LOG 2>&1
$ORACLE_HOME_LISTNER/bin/lsnrctl start PROD >> $LOG 2>&1 &
VER10LIST=`$ORACLE_HOME_LISTNER/bin/lsnrctl version | grep "LSNRCTL for " | cut -d' ' -f5 | cut -d'.' -f1`
export VER10LIST
else
echo "Failed to auto-start Oracle Net Listener using $ORACLE_HOME_LISTNER/bin/tnslsnr"
fi
fi
same as edit the dbshut file
#PROD
# The this to bring down Oracle Net Listener
ORACLE_HOME_LISTNER=/u01/app/oracle/product/11.2.0/PROD
if [ ! $ORACLE_HOME_LISTNER ] ; then
echo "ORACLE_HOME_LISTNER is not SET, unable to auto-stop Oracle Net Listener"
echo "Usage: $0 ORACLE_HOME"
else
LOG=$ORACLE_HOME_LISTNER/listener.log
# Set the ORACLE_HOME for the Oracle Net Listener, it gets reset to
# a different ORACLE_HOME for each entry in the oratab.
export ORACLE_HOME=$ORACLE_HOME_LISTNER
# Stop Oracle Net Listener
if [ -f $ORACLE_HOME_LISTNER/bin/tnslsnr ] ; then
echo "$0: Stoping Oracle Net Listener" >> $LOG 2>&1
$ORACLE_HOME_LISTNER/bin/lsnrctl stop PROD >> $LOG 2>&1 &
else
echo "Failed to auto-stop Oracle Net Listener using $ORACLE_HOME_LISTNER/bin/tnslsnr"
fi
fi