Skip to content

Instantly share code, notes, and snippets.

@nohtyp
Created July 16, 2014 09:08
Show Gist options
  • Select an option

  • Save nohtyp/76e281f8f1fb1d1cef8a to your computer and use it in GitHub Desktop.

Select an option

Save nohtyp/76e281f8f1fb1d1cef8a to your computer and use it in GitHub Desktop.
This gist is a shutdown and startup script for oracle.
#!/bin/bash
#dbora Oracle Database script
#
# chkconfig: 345 85 35
# description: This script starts and stops the Oracle databases
# and creates an output file of the returned messages
#
#
#author: Thomas Foster
#co-author: Brian Doran
#date: 06/06/13
start_listener()
{
#Loop through listeners and start them in order
for i in $MYLIST;
do
export ORACLE_HOME=$(grep $i $LSFILE|awk -F":" '{print $1}')
su $ORA_OWNER -c "$ORACLE_HOME/bin/lsnrctl status $i"
done
}
stop_listener()
{
#Check if listener file is in oracle's home location and emptys it if found
if [ -f "$LSFILE" ];then
> "$LSFILE"
else
$TCOMMAND "$LSFILE"
fi
#dump a list of all of the listeners and paths to the listener file in oracle's home directory
echo -ne "$LSTNHOME" >> "$LSFILE"
for i in $LSTN;
do
MYHOME=$(grep -w $i $LSFILE| awk -F":" '{print $1}')
echo "Stopping listener $i"
export ORACLE_HOME=$MYHOME
su $ORA_OWNER -c "$ORACLE_HOME/bin/lsnrctl status $i"
done
}
stop_db()
{
#zero out running database file
if [ -f "$DBFILE" ];then
> $DBFILE
else
$TCOMMAND "$DBFILE"
fi
#Shutdown the databases grab the running processes first
for i in $(ps -ef |grep _pmon | grep -v grep |awk -F " " '{print $8}' |awk -F "_" '{print $3}');
do
echo $i >> $DBFILE
export ORACLE_HOME=$(grep -w $i $OTAB |awk -F":" '{print $2}')
export ORACLE_SID=$(grep -w $i $OTAB |awk -F":" '{print $1}')
DBOFF=$(grep -w $i $OTAB | awk -F":" '{print $1}')
if [ "$DBOFF" == "#$i" ];then
echo "You have commented out the database instance $i in /etc/oratab..not shutting down"
else
echo "Shutting down database: $i"
echo "Setting oracle_home to $ORACLE_HOME for $i where oracle_sid is $ORACLE_SID"
#su $ORA_OWNER -c "$ORACLE_HOME/bin/sqlplus / as sysdba < /home/oracle/.shutdown.sql"
fi
done
}
start_db()
{
#List all of the databases in oratab and compare them to what was running. If running at time of shutdown
#and the startup is AUTO=Y then start it else log that server wsa not set to auto and do not start.
for i in $DBORATAB
do
RINST=`grep -w $i $DBFILE`
ASTART=$(grep -w $i $OTAB |awk -F":" '{print $3}' | cut -f 1)
DBOFF=$(grep -w $i $OTAB | awk -F":" '{print $1}')
if [ -z "$RINST" ];then
echo "The DB instance $i wasn't running when the database was stopped"
elif [ ! -z "$RINST" ] && [ "$ASTART" == 'Y' ];then
echo "DB instance $i was running and starting now.."
export ORACLE_HOME=$(grep -w $i $OTAB |awk -F":" '{print $2}')
export ORACLE_SID=$(grep -w $i $OTAB |awk -F":" '{print $1}')
#su $ORA_OWNER -c "$ORACLE_HOME/bin/sqlplus / as sysdba < /home/oracle/.startup.sql"
elif [ "$DBOFF" == "#$i" ];then
echo "You have commented out the database instance $i in /etc/oratab..not shutting down"
else
echo "The server database instance $i will not be started as AUTOSTART in the /etc/oratab file has not been set.."
fi
done
}
OTAB='/etc/oratab'
DBFILE='/home/oracle/.running_databases'
LSFILE='/home/oracle/.listener_processes'
DBORATAB=`egrep -v '$#|^#' $OTAB | grep -v '^$' | awk -F":" '{print $1}'`
TCOMMAND='/bin/touch'
/bin/touch "$LSFILE"
MYLIST=`awk -F":" '{print $2}' $LSFILE`
LSTN=`ps -ef | grep tnslsnr | egrep -v "grep|sed" | awk -F" " '{print $9}'`
LSTNHOME=`ps -ef | grep tnslsnr | egrep -v "grep|sed" | awk -F" " '{print $8":"$9}' |sed 's/bin\/tnslsnr//'`
ORA_OWNER='oracle'
case $1 in
start) start_listener
start_db;;
stop) stop_db
stop_listener;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment