Sie sind auf Seite 1von 4

#!

/bin/bash

###################################################################################
################
# Script to Bounce All the Running Databases/Listeners on a host
# Usage : dblsnr_bounce.sh <start | stop>
# stop -> stop all running databases and
listeners
# start -> start all databases and listeners
# Author : Idhasoft DBA-24x7
###################################################################################
################

if [ $# -lt 2 ]
then
echo "Please Specify an Operation"
echo "Usage: dblsnr_bounce.sh <start | stop> <instance_names>"
echo " stop -> stop running database and listener for given
instance_name"
echo " start -> start database and listener for given
instance_name "
echo " instance_name(s) -> one or more instance names can be
specified "
exit
fi

#Prepare Temporary Staging Directory


STAGE=/tmp
mkdir -p ${STAGE}
mkdir -p ${STAGE}/logs
mkdir -p ${STAGE}/.temp
cd ${STAGE}

DT=`date +%d%m%Y_%H%M`
ORATAB=/etc/oratab
export STAGE DT ORATAB

if [ "$1" == "stop" ]
then
{
_x=1
for _i in $@
do
if [ ${_x} -gt 1 ]
then
#Verify if instance_names provided to script are valid
ORACLE_SID=`cat /etc/oratab | grep -v \# | grep -i :N |
awk -F ":" '{print $1}'| grep ${_i}`
ORACLE_HOME=`cat /etc/oratab | grep -v \# | grep ${_i} |
grep -i :N | awk -F ":" '{print $2}'`
if [ `cat /etc/oratab | grep -v \# | grep -i :N | awk -F
":" '{print $1}' | grep ${_i} |wc -l` -eq 0 ]
then
echo -e
"\n########################################"
echo -e "No Entry in oratab for ${_i} !!!"
echo -e "Continuing with rest of instances..."
echo -e
"########################################\n"
continue
fi
PATH=${ORACLE_HOME}/bin:$PATH
LD_LIBRARY_PATH=${ORACLE_HOME}/lib
LSNR_NAME=`ps -ef | grep tns | grep -v grep | grep $
{ORACLE_HOME} | awk '{print $9}'`

export ORACLE_SID ORACLE_HOME PATH LD_LIBRARY_PATH


LSNR_NAME

###################################################################################
##
# Stop Listener

###################################################################################
##

echo -e
"####################################################"
echo -e "\nAttempting to Shut Down Listener ${LSNR_NAME}"

lsnrctl stop ${LSNR_NAME}


if [ $? -eq 0 ]
then
echo "Listener ${LSNR_NAME}
Shutdown Successfully."

else
echo -e "\n\tShutting down
of ${LSNR_NAME} Listener failed !"
echo -e "\n\tCheck log for
details"
fi

###################################################################################
##
# Stop Database

###################################################################################
##

echo -e
"####################################################"
echo -e "\n\nAttempting to Shutdown
Database: ${ORACLE_SID}"
sqlplus -s / as sysdba <<- HDOC
SELECT NAME, OPEN_MODE FROM V\$DATABASE;
SHUTDOWN IMMEDIATE
exit;
HDOC

DB_STATUS=`ps -ef | grep -v grep | grep


pmon | grep ${ORACLE_SID} | wc -l`
if [ ${DB_STATUS} -eq 0 ]
then
echo -e "\n\tDatabase ${ORACLE_SID}
Shutdown Successful"
else
echo -e "\nThere is problem
shutting down Database ${ORACLE_SID}"
fi

echo -e "\nLogfile for this execution-> $


{STAGE}/log/shutdown_log_${DT}.log"
fi
_x=$(( _x+1 ))
done

} 2>&1 | tee ${STAGE}/log/shutdown_log_${DT}.log

elif [ "$1" == "start" ]


then
{
_x=1
for _i in $@
do
if [ ${_x} -gt 1 ]
then
#Verify if instance_names provided to script are valid
ORACLE_SID=`cat /etc/oratab | grep -v \# | grep -i :N |
awk -F ":" '{print $1}'| grep ${_i}`
ORACLE_HOME=`cat /etc/oratab | grep -v \# | grep ${_i} |
grep -i :N | awk -F ":" '{print $2}'`
if [ `cat /etc/oratab | grep -v \# | grep -i :N | awk -F
":" '{print $1}' | grep ${_i} |wc -l` -eq 0 ]
then
echo -e
"\n########################################"
echo -e "No Entry in oratab for ${_i} !!!"
echo -e "Continuing with rest of instances..."
echo -e
"########################################\n"
continue
fi

PATH=${ORACLE_HOME}/bin:$PATH
LD_LIBRARY_PATH=${ORACLE_HOME}/lib
LSNR_NAME=`cat ${ORACLE_HOME}/network/admin/listener.ora |
grep SID_LIST | awk -F "=" '{print $1}' | sed 's|SID_LIST_||'`

export ORACLE_SID ORACLE_HOME PATH LD_LIBRARY_PATH


LSNR_NAME

###################################################################################
##
# Start Database

###################################################################################
##

echo -e
"####################################################"
echo -e "\nAttempting to Startup Database:
${ORACLE_SID}"
sqlplus -s / as sysdba <<- HDOC
STARTUP
SELECT NAME, OPEN_MODE FROM V\$DATABASE;
exit;
HDOC

echo -e "\n\tDatabase ${ORACLE_SID} Started


Successfully"

###################################################################################
##
# Start Listener

###################################################################################
##
echo -e
"####################################################"
echo -e "\n\nAttempting to Start Listener ${LSNR_NAME}"

lsnrctl start ${LSNR_NAME}


if [ $? -eq 0 ]
then
echo "Listener ${LSNR_NAME}
Started Successfully."
else
echo -e "\n\Starting up of
${LSNR_NAME} Listener failed !"
echo -e "\n\tCheck log for
details"
fi

echo -e "\nLogfile for this execution-> $


{STAGE}/log/startup_log_${DT}.log"
fi
_x=$(( _x+1 ))
done

} 2>&1 | tee ${STAGE}/log/startup_log_${DT}.log

else
echo "Please Specify an Operation"
echo "Usage: dblsnr_bounce.sh <start | stop> <instance_name(s)>"
echo " stop -> stop all running databases and listeners"
echo " start -> start all databases and listeners"
echo " instance_name(s) -> one or more instance names can be
specified "
exit
fi