Shell Script to automate Oracle Database and Listener Startup/Shutdown on Linux

By default, Oracle software installation does not deploy automatic startup and shutdown init scripts on the platform.It’s desirable to have the Oracle database and listener automatically shutdown and startup when the server reboots unexpectedly.

Step 1.  Edit the /etc/oratab file, and place a Y at the end of the entry for the databases you want to automatically restart when the system reboots. This might need root privileges to edit the file:

#vi /etc/oratab

DEMO:/opt/oracle/product/12.2.0/db_1:Y

The Y on the end of the string signifies that the database can be tarted and stopped by the $ORACLE_HOME/bin/dbstart and $ORACLE_HOME/bin/dbshut scripts.

Step 2. Create the service script /etc/init.d/dbora. Content of the script is as follows.We need to change the values of variables ORACLE_HOME and ORACLE_OWNER as per our environment. This is a bare bones script for stop and start a database and listener:

# vi /etc/init.d/dbora

#!/bin/sh

ORACLE_HOME=/opt/oracle/product/12.2.0/db_1

ORACLE_OWNER=oracle

case "$1" in

'start') # Start the Oracle databases and listeners

su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/lsnrctl start LISTENER"

su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME"

;;

'stop') # Stop the Oracle databases and listeners

su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/lsnrctl start LISTENER"

su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME"

;;

esac

# End of script dbora

Step 3. Adding this init script to rc0, rc3 and rc5

We use 3 soft links to add them into different running levels and prioritize the execution orders by naming the file.

# chgrp dba dbora

# chmod 750 dbora

# ln -s /etc/init.d/dbora /etc/rc.d/rc0.d/K01dbora

# ln -s /etc/init.d/dbora /etc/rc.d/rc3.d/S99dbora

#ln -s /etc/init.d/dbora /etc/rc.d/rc5.d/S99dbora

 Step 4. For testing the script:

Restart the whole server

# init 6

For manually start/stop DB and listener:

A: To test whether the dbora script is working, as root run the following to stop your database and listener:

# /etc/init.d/dbora stop

B. To test the startup of your database and listener, as root issue the following command:

# /etc/init.d/dbora start

 ==========================Thank You============


No comments:

Post a Comment

thedbaportfolio@gmail.com