Sunday, January 4, 2015

Oracle - Restart Auto on Reboot

On Linux Reboot Oracle Instance and Listener will restart automatically
=====================================================
Note : Change Oracle Home and Oracle SID  in the following


1) Create a directory to hold logfile and Script our case /apps/oracle/bin
   mkdir /apps/oracle/bin/
   Now this directory will hold
   a) dbshut.sh
   b) dbstart.sh
   Note1 : above two files are owned by oracle:oinstall
   Note2 : See Scripts at the Bottom of this Notes

2) Create Damon a file in /etc/init.d/dboracle

vi /etc/init.d/dboracle
-------------------------
#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
ORA_OWNER=oracle
LOG_FILE=/apps/oracle/bin/dbstart_dbshut.log
case "$1" in
    'start')
        echo "Starting Oracle... log location $LOG_FILE"
        date +"Date.. %D %T %A " >> $LOG_FILE
        su $ORA_OWNER -c /apps/oracle/bin/dbstart.sh >> $LOG_FILE
        touch /var/lock/subsys/dbora
        echo "Done.."
        ;;
    'stop')
        echo "Stoping Oracle... log location $LOG_FILE"
        date +"Date.. %D %T %A " >>  $LOG_FILE
        su $ORA_OWNER -c /apps/oracle/bin/dbshut.sh >> $LOG_FILE
        rm -f /var/lock/subsys/dbora
        echo "Done.."
        ;;
     *)
       echo "Usage :dboracle {start|stop}"
       exit 1
esac
---------------------------------------------------

3)as root#
   chmod 755 /etc/init.d/dboracle

3) Now you can test this by
 
  root# service dboracle stop
  root# service dboracle start

4) If you need to register this with linux auto start then
  
   chkconfig --add dboracle
   now on reboot  Linux will gracefully stop service and start service automatically

======= SCRIPTS ======

---- dbshut.sh --------------------------------
vi /apps/oracle/bin/dbshut.sh
#!/bin/bash
export ORACLE_HOME=/apps/oracle/product/11.2.0/db
export PATH=/usr/sbin:/usr/local/bin:$ORACLE_HOME:ORACLE_HOME/bin:$PATH
export ORACLE_SID=GRIDDB
ORAENV_ASK=NO
. oraenv
ORAENV_ASK=YES
# Stop Database
sqlplus / as sysdba << EOF
SHUTDOWN IMMEDIATE;
EXIT;
EOF
# Stop Listener
lsnrctl stop
echo "Done.."

---- dbstart.sh --------------------------------

vi /apps/oracle/bin/dbstart.sh
#!/bin/bash
export ORACLE_HOME=/apps/oracle/product/11.2.0/db
export PATH=/usr/sbin:/usr/local/bin:$ORACLE_HOME:ORACLE_HOME/bin:$PATH
export ORACLE_SID=GRIDDB
ORAENV_ASK=NO
. oraenv
ORAENV_ASK=YES
# Start Listener
lsnrctl start
# Start Database
sqlplus / as sysdba << EOF
STARTUP;
EXIT;
EOF
echo "Done.."

----------------------- Change Some Permissions -------------------


chown oracle:oinstall /apps/oracle/bin/dbstart.sh
chown oracle:oinstall /apps/oracle/bin/dbshut.sh
touch /apps/oracle/bin/dbstart_dbshut.log
chown oracle:oinstall /apps/oracle/bin/dbshut.sh
chmod 755 dbshut.sh
chmod 755 dbstart.sh


Thanks
Adarsh Kumar
AK Technosoft Corp.
www.aktechnosoft.com