Oracle 10g-11g Automated Startup and Shutdown on RedHat

Oracle 10g/11g  Automated Startup and Shutdown on RedHat 

Create file /etc/init.d/oracle with following code and change file to be executable
 
#!/bin/sh
# chkconfig: 345 99 01
# description: Oracle
#
#
ORACLE=oracle
case $1 in
'start')
        cat <<-"EOF"|su - ${ORACLE}
        # Start Oracle Net
        if [ -f ${ORACLE_HOME}/bin/tnslsnr ] ;
        then
                echo "starting Oracle Net Listener"
                ${ORACLE_HOME}/bin/lsnrctl start
        fi
        echo "Starting Oracle databases"
        ${ORACLE_HOME}/bin/dbstart
        ${ORACLE_HOME}/bin/emctl start dbconsole
EOF
        ;;
'stop')
        cat <<-"EOF"|su - ${ORACLE}
        echo "shutting down dbconsole"
        ${ORACLE_HOME}/bin/emctl stop dbconsole
        # Stop Oracle Net
        if [ -f ${ORACLE_HOME}/bin/tnslsnr ] ;
        then
                echo "stopping Oracle Net Listener"
                ${ORACLE_HOME}/bin/lsnrctl stop
        fi
        echo "stopping Oracle databases"
        ${ORACLE_HOME}/bin/dbshut
EOF
        ;;
*)
        echo "usage: $0 {start|stop}"
        exit
        ;;
esac
#
exit
 
Run:
chkconfig oracle on
 
Update the oracle user .bash_profile as follows:
export ORACLE=oracle
export ORACLE_SID=`cat /etc/oratab |sed -e 's/:.*//' -e 's/#.*//' -e '/^$/d'|head -1`
export PATH=$PATH:/usr/local/bin
export ORAENV_ASK="NO"
. /usr/local/bin/oraenv
 
Update /etc/oratab with your instances
orcl:/u01/oracle/product/11.1.0/db_1:Y
orcltest:/u01/oracle/product/11.1.0/db_1:Y

Alternatively if you want to use your own start scripts you could do the following (BUT WHY?):
 
Create an /etc/init.d/oracle script with:
#!/bin/sh
#
#oracle agent init script
#chkconfig: 2345 97 05
#description: oracle
# Source function library.
if [ -f /etc/init.d/functions ] ; then
        . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
        . /etc/rc.d/init.d/functions
else
        exit 0
fi
prog=oracle
ORAHOME=/oracle/home/scripts
AGENT_USER=oracle
email=pvalentino@sysxperts.com
start () {
        echo -n $"Starting $prog: "
        # start daemon
        if [ -e "/tmp/orastat" ]
        then
        su - ${AGENT_USER} -c "cd ${ORAHOME}; ./orastart"
        rm -rf /tmp/orastat
        else
        mail -s "`hostname` orastart failed" $email < /tmp/stat
        fi
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/oracle
        return $RETVAL
}
stop () {
        # stop daemon
        echo -n $"Stopping $prog: "
        su - ${AGENT_USER} -c "cd ${ORAHOME};./orastop"
        RETVAL=$?
        if [[ "$RETVAL" = 0 ]] ;then touch /tmp/orastat;else mail -s "`hostname` orastop failed" $email < /tmp/stat;fi
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/oracle
        return $RETVAL
}
restart() {
        stop
        start
}
case $1 in
        start)
                start
        ;;
        stop)
                stop
        ;;
        restart|reload)
                restart
        ;;
        condrestart)
                [ -f /var/lock/subsys/ora ] && restart || :
        ;;
        *)
        echo $"Usage: $prog {start|stop|restart|condrestart|reload }"
        exit 1
esac
exit $RETVAL

And your orastart and orastop scripts would have all of the startup procedures you would like to run in a custom fashion i.e.
 
orastart:
. /home/oracle/scripts/orastart_TEST
lsnrctl start
#
mail -s "****** TEST databases started *****" _DBA@sysxperts.com < /home/oracle/scripts/orastart
 
 
orastart_TEST:
. /home/oracle/ora10.env
############# This will start Oracle in TEST ######################
export ORACLE_SID=TEST
sqlplus '/ as sysdba' <<EOF
startup
EOF
#
 

No comments: