8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Linux » Here

Automating Database Startup and Shutdown on Linux

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.

These methods work on all RHEL and Oracle Linux versions up to and including RHEL7/OL7.

Related articles.

What I Use

This article contains a number of variations, but this is what I currently use, which is a variation on the "su" command.

The scripts are created using the cat command, with all the "$" characters escaped. If you want to manually create these files, rather than using the cat command, remember to remove the "\" characters before the "$" characters.

Create a "scripts" directory.

mkdir /home/oracle/scripts

Create an environment file called "setEnv.sh". This is an example from a 12.2 installation. Adjust the contents according to your installation.

cat > /home/oracle/scripts/setEnv.sh <<EOF
# Oracle Settings
export TMP=/tmp
export TMPDIR=\$TMP

export ORACLE_HOSTNAME=ol7-122.localdomain
export ORACLE_UNQNAME=cdb1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=\$ORACLE_BASE/product/12.2.0.1/db_1
export ORACLE_SID=cdb1

export PATH=/usr/sbin:/usr/local/bin:\$PATH
export PATH=\$ORACLE_HOME/bin:\$PATH

export LD_LIBRARY_PATH=\$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=\$ORACLE_HOME/jlib:\$ORACLE_HOME/rdbms/jlib
EOF

Add a reference to the "setEnv.sh" file at the end of the "/home/oracle/.bash_profile" file if you want the settings to be applied for a normal login. The profile will not be set during the start/stop of a service, so this is not necessary for the automatic start/stop functionality.

echo ". /home/oracle/scripts/setEnv.sh" >> /home/oracle/.bash_profile

Create a "start_all.sh" and "stop_all.sh" script that can be called from a startup/shutdown service. Make sure the ownership and permissions are correct.

cat > /home/oracle/scripts/start_all.sh <<EOF
#!/bin/bash
. /home/oracle/scripts/setEnv.sh

export ORAENV_ASK=NO
. oraenv
export ORAENV_ASK=YES

dbstart \$ORACLE_HOME
EOF


cat > /home/oracle/scripts/stop_all.sh <<EOF
#!/bin/bash
. /home/oracle/scripts/setEnv.sh

export ORAENV_ASK=NO
. oraenv
export ORAENV_ASK=YES

dbshut \$ORACLE_HOME
EOF

chown -R oracle.oinstall /home/oracle/scripts
chmod u+x /home/oracle/scripts/*.sh

You should be able to start/stop the database with the following scripts run from the "oracle" user.

$ ~/scripts/start_all.sh
$ ~/scripts/stop_all.sh

Now we need to create the Linux service to call the scripts we created previously. The reset of this section represents what I so for OL6, but it will also work for OL7. If you are using OL7 and prefer to use systemd directly, you can follow the instructions provided here.

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

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_OWNER to the user id of the owner of the 
# Oracle database software.

ORA_OWNER=oracle

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 "/home/oracle/scripts/start_all.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1" &

        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 "/home/oracle/scripts/stop_all.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1"
        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

You can start and stop the database using the service, which is what will happen on a reboot.

# service dbora start
# service dbora stop

systemd Services

With the introduction of RHEL7/OL7, services are now managed using systemd. You can continue to use the existing methods shown below for creating a service to auto-start Oracle, as systemd is backwards compatible. If you prefer to use systemd directly, you can follow the instructions provided here.

The systemd example assumes you have the scrips defined above in the "/home/oracle/scripts/" directory present.

The "su" Command

The following method for automating database startup and shutdown of Oracle instances on Linux works equally well for Oracle 9i, 10g, 11G and 12c. It can be used on any RHEL-style distribution, including Oracle Linux, up to an including RHEL7. I still use this method for Oracle 12c on OL6. It will work for RHEL7/OL7, but I prefer to use the systemd services.

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

TSH1:/u01/app/oracle/product/12.2.0.1/db_1:Y

Create a file called "/etc/init.d/dbora" as the root user, containing the following code. Adjust the paths to match your system.

#!/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/10.2.0/db_1
#ORA_HOME=/u01/app/oracle/product/11.1.0/db_1
#ORA_HOME=/u01/app/oracle/product/11.2.0.4/db_1
#ORA_HOME=/u01/app/oracle/product/12.1.0.2/db_1
ORA_HOME=/u01/app/oracle/product/12.2.0.1/db_1
ORA_OWNER=oracle
export ORACLE_UNQNAME=db12c

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/dbstart $ORA_HOME" &
        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 $ORA_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

The relevant instances should now startup/shutdown automatically at system startup/shutdown.

For Oracle 9i the dbstart and dbshut commands didn't control the listener, so listener management had to be done separately, as shown below.

#!/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/9.2.0
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

The "rsh" Command

Some of the Oracle 10g documentation recommends using the "rsh" command in the "dbora" service. Later database versions switched back to using the "su" command. I have never liked or used this approach on a real system.

With Oracle 10g, Oracle switched from recommending the "su" command to the "rsh" command. In Oracle 10g release 2, the dbstart command includes an automatic start of the listener, so there are some differences between the two versions, but the following represents Oracle's preferred method for Oracle 10g.

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

TSH1:/u01/app/oracle/product/10.2.0:Y

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

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Change the value of ORACLE_HOME to specify the correct Oracle home
# directory for your installation.

ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
#
# Change the value of ORACLE to the login name of the
# oracle owner at your site.
#
ORACLE=oracle

PATH=${PATH}:$ORACLE_HOME/bin
HOST=`hostname`
PLATFORM=`uname`
export ORACLE_HOME PATH
#
if [ ! "$2" = "ORA_DB" ] ; then
   if [ "$PLATFORM" = "HP-UX" ] ; then
      remsh $HOST -l $ORACLE -n "$0 $1 ORA_DB"
      exit
   else
      rsh $HOST -l $ORACLE  $0 $1 ORA_DB
      exit
   fi
fi
#
case $1 in
'start')
        $ORACLE_HOME/bin/dbstart $ORACLE_HOME
        touch /var/lock/subsys/dbora
        ;;
'stop')
        $ORACLE_HOME/bin/dbshut $ORACLE_HOME
        rm -f /var/lock/subsys/dbora
        ;;
*)
        echo "usage: $0 {start|stop}"
        exit
        ;;
esac
#
exit

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

The relevant instances should now startup/shutdown automatically at system startup/shutdown.

This method relies on the presence of an RSH server, which requires additional packages and configuration.

# Install the rhs and rsh-server packages from the OS CD/DVD.
rpm -Uvh --force rsh-*

# Enable rsh and rlogin.
chkconfig rsh on
chkconfig rlogin on
service xinetd reload

This can be quite problematic when attempting to use this method under later Linux distributions, where rsh is deprecated. As a result, I prefer to use the "su" command method.

This method can also be used for 11g databases that are not using ASM or RAC.

The "runuser" Command

For a time the Oracle 12c documentation recommended using the "runuser" command in the "dbora" service. The latest version of the documents have reverted the using the "su" command. An example of using the "runuser" command is shown below, but I don't use this.

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

DB12C:/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, which is a modified version of the example from the documentation, which doesn't work.

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Change the value of ORACLE_HOME to specify the correct Oracle home
# directory for your installation.

ORACLE_HOME=/u01/app/oracle/product/12.1.0.2/db_1
#
# Change the value of ORACLE to the login name of the
# oracle owner at your site.
#
ORACLE=oracle
PATH=${PATH}:$ORACLE_HOME/bin
export ORACLE_HOME PATH
#
case $1 in
'start')
        runuser -l $ORACLE -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME &"
        touch /var/lock/subsys/dbora
        ;;
'stop')
        runuser -l $ORACLE -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME"
        rm -f /var/lock/subsys/dbora
        ;;
*)
        echo "usage: $0 {start|stop}"
        exit
        ;;
esac
#
exit

If you want the service to wait while the startup completes, remove the "&". This is especially important for shutdowns that take a long time, like when shutting down WebLogic and Cloud Control services.

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

chmod 750 /etc/init.d/dbora
chkconfig --add dbora

Known Issues

When using Oracle 10g Release 2, calling dbstart without the "$ORACLE_HOME" might result in the following error message.

Failed to auto-start Oracle Net Listener using /ade/vikrkuma_new/oracle/bin/tnslsnr

This is due to a hard coded path in the dbstart script. You should not see this error if you pass the "$ORACLE_HOME" as a parameter to dbstart and dbshut. To correct this, edit the "$ORACLE_HOME/bin/dbstart" script and replace the following line (approximately line 78).

ORACLE_HOME_LISTNER=/ade/vikrkuma_new/oracle

With this.

ORACLE_HOME_LISTNER=$ORACLE_HOME

The dbstart script should now start the listener as expected.

dbstart and dbshut Deprecation?

The Oracle 11gR2 documentation states the use of the dbstart and dbshut scripts are deprecated. The preferred replacement is Oracle Restart.

Both dbstart and dbshut are still present in Oracle 11gR2, so you can continue to use them (I still use them). In order to use Oracle Restart you must install Grid Infrastructure (GI), which you will already have if you are using RAC or ASM for a standalone instance. In these cases, Oracle Restart will already be present and running. For single instance databases that don't use ASM, I think it is unreasonable to expect people to install GI.

The Oracle 12c documentation has no mention of the deprecation of dbstart and dbshut and has reinstated the documentation about them. As a result, you are free to use dbstart and dbshut in a supported manner for all versions of the database.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.