Monday, April 4, 2011

Automate Startup and Shutdown of Oracle Database on Linux Machines

This blog explains my own scripts which I use to startup and shutdown the database server.
I don't use the oracle provided scripts given under $ORACLE_HOME/bin (dbstart/dbshut) but completely do it on my own.
The following script can be used to start an oracle database, listener (provided the listener name is same as the oracle db name) and management agent. The script does not start or shutdown ASM instance.
If anybody wants I will write a script for you.Please feel free to email me as I will gather some extra motivation if somebody contacts me :-) .

But before I use my 2 scripts, please make sure the following 2 files are present

1. /etc/oratab - This file should be familiar as this is created with the oracle installation and the above mentioned dbstart and dbshut use this file. i find it convenient to use the same file. This file is used to help me know the ORACLE_SID, ORACLE_HOME env variables.
A typical /etc/oratab looks like this


orcl:/oracle/software/rdbms/10.2.0.4.0:Y -->entry for database and a listener with the same name
agent:/oracle/software/agent/10.1.0.5:N --> entry for oracle agent

Please note that I install Management agent in all my databases and therefore create an entry for the Agent oracle home too.The command to start and shutdown the agent is (the script already handles this but I have just mentioned it for the knowledge of some people who are not aware about this).

Please note that even if you don't have an agent installed , you can still use the script below.
emctl start agent
emctl stop agent

2.The presence of a script called sid under /usr/local/bin . Please note that /usr/local/bin is a directory under default PATH of mose unix users. So I prefer to put the script sid under this directory.

Here is the contents of sid script
$ cat /usr/local/bin/sid
#!/bin/sh
isthere=`cat /etc/oratab | grep -v ^#|grep -i $1|wc -l`
if  [ $isthere -eq 1 ] ; then

export TNS_ADMIN=/etc
export NLS_LANG=AMERICAN_AMERICA.UTF8
export ORACLE_SID=`cat /etc/oratab | grep -v ^#|grep -i $1|awk 'BEGIN{FS=":"} {print $1}'`
export ORACLE_HOME=`cat /etc/oratab | grep -v ^#|grep -i $1|awk 'BEGIN{FS=":"} {print $2}'`
export PATH=$ORACLE_HOME/bin:$PATH:/usr/local/bin:.
echo "Oracle SID set to $ORACLE_SID"
echo "Oracle Home set to $ORACLE_HOME"

elif [ $isthere -eq 0 ] ; then
echo "Unable to find any matching rows in /etc/oratab"
else
echo "Too many matching rows in /etc/oratab"
fi


The above script is invoked as
$ . /usr/local/bin/sid orcl
Oracle SID set to orcl
Oracle Home set to /oracle/software/rdbms/10.2.0.4.0
$ . /usr/local/bin/sid nothing
Unable to find any matching rows in /etc/oratab
$


After this we are all set to go to the main shell script. I name the script as dbora.Please feel free to name it anything you want.

Do the following

1)Please copy this file to /etc/init.d directory as dbora
2)chgrp dba dbora
3)chmod 750 dbora
4)/sbin/chkconfig –add dbora
5)create a directory called /oracle/admin/logs as the below script will write to this directory.If you plan to change the directory please feel free to edit  DB_LOG_DEST in the below shell script.


Here are the contents of the shell script

++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/ksh
# chkconfig: 35 99 10
ORA_OWNER=oracle
DB_LOG_DEST="/oracle/admin/logs"


case "$1" in
'start')
rm /tmp/dbstart.sh 2>/dev/null

cat /etc/oratab|grep -v ^#|grep -v ^$|while read LINE ; do
AUTO_RS=`echo $LINE | awk 'BEGIN {FS=":"} {print $3}'`
#echo $AUTO_RS

if [ ${AUTO_RS} = "Y" ]; then



ORA_SID=`echo $LINE | awk 'BEGIN {FS=":"} {print $1}'`



if [ $ORA_SID = "agent" ]; then
echo ". /usr/local/bin/sid $ORA_SID" >>/tmp/dbstart.sh
echo  "emctl start agent" >>/tmp/dbstart.sh
else
echo ". /usr/local/bin/sid $ORA_SID" >>/tmp/dbstart.sh
echo "lsnrctl start $ORA_SID" >>/tmp/dbstart.sh
echo ". /usr/local/bin/sid $ORA_SID" >>/tmp/dbstart.sh
cat<<EOF1 >>/tmp/dbstart.sh
sqlplus "/ as sysdba" <<EOF
startup;
EOF
EOF1
fi



fi

done

echo "##################################">> $DB_LOG_DEST/DBSTART_STOP.log
echo "Starting up databases on `date`" >>   $DB_LOG_DEST/DBSTART_STOP.log
echo "##################################">> $DB_LOG_DEST/DBSTART_STOP.log
chown oracle:dba /tmp/dbstart.sh
chmod 700 /tmp/dbstart.sh
su - $ORA_OWNER -c /tmp/dbstart.sh >> $DB_LOG_DEST/DBSTART_STOP.log

;;
'stop')
rm /tmp/dbstart.sh 2>/dev/null

cat /etc/oratab|grep -v ^#|grep -v ^$|while read LINE ; do
AUTO_RS=`echo $LINE | awk 'BEGIN {FS=":"} {print $3}'`
#echo $AUTO_RS

if [ ${AUTO_RS} = "Y" ]; then



ORA_SID=`echo $LINE | awk 'BEGIN {FS=":"} {print $1}'`



if [ $ORA_SID = "agent" ]; then
echo ". /usr/local/bin/sid $ORA_SID" >>/tmp/dbstart.sh
echo  "emctl stop agent" >>/tmp/dbstart.sh
else
echo ". /usr/local/bin/sid $ORA_SID" >>/tmp/dbstart.sh
echo "lsnrctl stop $ORA_SID" >>/tmp/dbstart.sh
echo ". /usr/local/bin/sid $ORA_SID" >>/tmp/dbstart.sh
cat<<EOF1 >>/tmp/dbstart.sh
sqlplus "/ as sysdba" <<EOF
shutdown immediate;
EOF
EOF1
fi



fi

done

echo "##################################">> $DB_LOG_DEST/DBSTART_STOP.log
echo "Starting up databases on `date`" >>   $DB_LOG_DEST/DBSTART_STOP.log
echo "##################################">> $DB_LOG_DEST/DBSTART_STOP.log
chown oracle:dba /tmp/dbstart.sh
chmod 700 /tmp/dbstart.sh
su - $ORA_OWNER -c /tmp/dbstart.sh >> $DB_LOG_DEST/DBSTART_STOP.log


;;
esac

++++++++++++++++++++++++++++++++++++++++++


Please note that I have put
# chkconfig: 35 99 10
In the script, which should mean that this service will start at runlevels 3 and 5.
99 indicates this service will be started near the end of init processing and 10 signifies it will be stopped near the beginning of init processing.
Please feel free to make changes.