Auto Start Oracle Database Server and Listener

From SmartHCM Wiki
Jump to navigation Jump to search

The following represents the Oracle recommended method for automating database startup and shutdown of Oracle 11g, 12c instances on Linux, but it works fine for Oracle other versions as well. It can be used on any RHEL-style distribution, including Oracle Linux, CentOS and etc.

Once the instance is created, edit the "/etc/oratab" file setting the restart flag for each instance to 'Y'.

smarthcm:/u01/app/oracle/product/12.1.0.2/db_1:Y

Create a file called "/etc/init.d/dbora" as the root user, containing the following code.

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_HOME to be equivalent to the $ORACLE_HOME
# from which you wish to execute dbstart and dbshut;
#
# Set ORA_OWNER to the user id of the owner of the 
# Oracle database in ORA_HOME.

ORA_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
ORA_OWNER=oracle

if [ ! -f $ORA_HOME/bin/dbstart ]
then
    echo "Oracle startup: cannot start"
    exit
fi

case "$1" in
    'start')
        # Start the Oracle databases:
        # The following command assumes that the oracle login 
        # will not prompt the user for any values
        # Remove "&" if you don't want startup as a background process.
        su $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start" &
        su $ORA_OWNER -c $ORA_HOME/bin/dbstart &
        touch /var/lock/subsys/dbora
        ;;
    'stop')
        # Stop the Oracle databases:
        # The following command assumes that the oracle login 
        # will not prompt the user for any values
        su $ORA_OWNER -c $ORA_HOME/bin/dbshut
        su $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop"
        rm -f /var/lock/subsys/dbora
        ;;
esac

In some environment, following code works instead of above;

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_HOME to be equivalent to the $ORACLE_HOME
# from which you wish to execute dbstart and dbshut;
#
# Set ORA_OWNER to the user id of the owner of the 
# Oracle database in ORA_HOME.

# path to oracle home (needed only to check if dbstart exists)
ORA_HOME=/u01/app/oracle/product/12.1.0.2/db_1
# this is the user who installed oracle
ORA_OWNER=oracle

if [ ! -f $ORA_HOME/bin/dbstart ]
then
    echo "Oracle startup: cannot start"
    exit
fi

case "$1" in
    'start')
        # Start the Oracle database and listener:
        # Remove "&" if you don't want startup as a background process.
        export ORACLE_SID=smarthcm
        export ORAENV_ASK=NO
        . /usr/local/bin/oraenv
        # at this point we have $ORACLE_HOME env variable set
        su $ORA_OWNER -c "dbstart $ORACLE_HOME" &
        touch /var/lock/subsys/dbora
        ;;
    'stop')
        # Stop the Oracle database and listener:
        export ORACLE_SID=smarthcm
        export ORAENV_ASK=NO
        . /usr/local/bin/oraenv
        su $ORA_OWNER -c "dbshut $ORACLE_HOME"
        rm -f /var/lock/subsys/dbora
        ;;
esac

Use the chmod command to set the privileges to 750.

chmod 750 /etc/init.d/dbora

Associate the "dbora" service with the appropriate run levels and set it to auto-start using the following command.

chkconfig --add dbora