Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dbctl - Start|Stop Databases at system boot.

Scripts

dbctl - Start|Stop Databases at system boot.

by  Michael42  Posted    (Edited  )
Code:
#!/bin/sh
# Purpose: Start|Stop Databases at system boot.
# Version: 3.01
# OS:      Sun Solaris 8-10
# Oracle:  9i-10g  
#
# Required steps to enable:
# -----------------------------------------------------------------------------
# 1. Log in as the root user.
# 2. Copy this file (dbctl) to the /etc/init.d directory.
# 3. Change the permissions: unix> chown root:dba dbctl
#                            unix> chmod 760 dbctl 
# 4. Create symbolic links to the dbctl script in the appropriate run-level dir.
#    unix> cd /etc/rc2.d
#    unix> ln -s /etc/init.d/dbctl /etc/rc2.d/K01dbctl
#    unix> cd /etc/rc3.d
#    unix> ln -s /etc/init.d/dbctl /etc/rc3.d/S99dbctl
################################################################################

########################################
# Init User Variables 
########################################
LISTENER_HOST="SUN01"            # Place your listener.ora HOST= value here.  This tests network availability.
DATABASES="DB1"                  # Space separated list of DBs  Ex: DATABASES="DB1 DB2"
SERVICES="DBCONSOLE LISTENER"    # Space separated list of containing valid services:
                                 # Oracle 10 Ex: LISTENER DBCONSOLE ISQLPLUS HTTPSRV AGENT10
                                 # Oracle 9i Ex: LISTENER APACHE AGENT9

WAIT="60"                        # Number of seconds to wait before running. "0" = No wait.
                                 # This is useful on systems where the resources may not
                                 # initially be available to Oracle upon a system boot.

DATA_DIR=/export/oradata         # Oracle should not come up if it's data directories are not
                                 # available.  Place one of the core data directories here.

DEBUG=F                          # If "T" just output settings and test for data directory and DNS. 
                                 
# Oracle Env
ORACLE_BASE="/home/oracle";                 export ORACLE_BASE
ORACLE_HOME="$ORACLE_BASE/product/10.2/db"; export ORACLE_HOME
LD_LIBRARY_PATH="$ORACLE_HOME/lib";         export LD_LIBRARY_PATH
LOGDIR="$ORACLE_BASE/admin"


################################################################################
################################################################################
###########         DO NOT CHANGE ANYTHING BELOW THIS LINE          ############
################################################################################
################################################################################

########################################
# Init System Vars and Envirnment
######################################## 
PATH=/bin:$PATH
SCRIPTNAME=`basename $0 .sh`
DATE_STAMP=`date '+%Y%m%d'`
TIME_STAMP=`date '+%H%M'`
DATE_START=`date`
MONTH=`date '+%m'`
# --------------------------------------
ORACLE_SID="-999"
USERID=`/usr/ucb/whoami`
unalias rm


########################################
# Init Log
########################################
rm -f $LOGDIR/$SCRIPTNAME.$1.log > /dev/null
touch $LOGDIR/$SCRIPTNAME.$1.log


########################################
# Header
########################################
clear
echo "===> $SCRIPTNAME started" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
echo "     ORACLE_BASE:     $ORACLE_BASE"
echo "     ORACLE_HOME:     $ORACLE_HOME"
echo "     LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
echo "     -----------------------------------------------"
echo "     CMD LINE:        $1"
echo "     DATABASES:       $DATABASES" 
echo "     SERVICES:        $SERVICES"
echo " "
echo "     WAIT:            $WAIT"
echo "     DATA_DIR:        $DATA_DIR"
echo "     DEBUG:           $DEBUG"
echo "\n\n"
sleep 3


########################################
# Dispatch Database Command
########################################
if [ "$1" = "start" ]; then
     CMD="startup"
elif [ "$1" = "stop" ]; then
     CMD="shutdown immediate"
else
     clear
     echo "$0 ERROR\n"                         2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
     echo "Invalid parameter used.\n"          2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
     echo "Usage: $SCRIPTNAME start|stop\n\n"  2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
     exit
fi 


########################################
# Test if user is root
########################################
if [ "$USERID" != "root" ]; then
   echo "Process aborted: Must be run as root" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
   exit
fi


########################################
# Wait
########################################
if [ "$1" = "start" ]; then
   if [ "$WAIT" != "0" ]; then
      echo "\n===> Waiting for $WAIT seconds before starting..." 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
      sleep $WAIT
   fi
fi


########################################
# Validate Data Directory
########################################
if [ "$1" = "start" ]; then
   if [ -d "$DATA_DIR" ]; then
      echo "\n===> Data directory detected..."                   2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
   else
      echo "\n===> System Error: Data directory not detected!!!" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
      exit
   fi
fi


########################################
# Debug
########################################
if [ "$DEBUG" = "T" -o "$DEBUG" = "t" ]; then
   exit
fi


#######################################
# Process: Databases 
########################################
echo "\n\n===> Processing Database Action: $CMD\n" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
for DB in $DATABASES
do
   echo "\n===> $DB" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
   ORACLE_SID="$DB"
   export ORACLE_SID

   # SQLPlus Session
   su - oracle -c "$ORACLE_HOME/bin/sqlplus /nolog" << SQLPLUS
   connect / as sysdba
   show user;
   spool $LOGDIR/$SCRIPTNAME.$DB.$1.spool
   $CMD
   exit
SQLPLUS

# Add Spooled Display to Log File
cat $LOGDIR/$SCRIPTNAME.$DB.$1.spool >> $LOGDIR/$SCRIPTNAME.$1.log
rm -f $LOGDIR/$SCRIPTNAME.$DB.$1.spool

done


########################################
# Process: Services
########################################
echo "\n\n===> Processing Services Action: $1" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
for SVC in $SERVICES
do
   echo "\n\n     $SVC ..." 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log    

   ### Common ###
   # Listener
   if [ "$SVC" = "LISTENER" -o "$SVC" = "listener" ]; then
      # Test LISTENER_HOST
      echo "     Pinging $LISTENER_HOST ..."               2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
      ping $LISTENER_HOST > /dev/null
      if [ $? -eq 0 ]; then
         echo "     $LISTENER_HOST found."                 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
      else
         echo "    $LISTENER_HOST not found!"              2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
      fi

      su - oracle -c "$ORACLE_HOME/bin/lsnrctl $1"         2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log  
   fi   

   ### 10g ###
   if [ "$SVC" = "DBCONSOLE" -o "$SVC" = "dbconsole" ]; then
      su - oracle -c "$ORACLE_HOME/bin/emctl $1 dbconsole" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log  
   fi
   if [ "$SVC" = "AGENT10" -o "$SVC" = "agent10" ]; then
      su - oracle -c "$ORACLE_HOME/bin/emctl $1 agent"     2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
   fi
   if [ "$SVC" = "ISQLPLUS" -o "$SVC" = "isqlplus" ]; then
      su - oracle -c "$ORACLE_HOME/bin/isqlplusctl $1"     2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
   fi


   ### 9i ###
   if [ "$SVC" = "AGENT9" -o "$SVC" = "agent9" ]; then
      su - oracle -c "$ORACLE_HOME/bin/agentctl $1"                2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
   fi
   if [ "$SVC" = "APACHE" -o "$SVC" = "apache" ]; then
      su - oracle -c "$ORACLE_HOME/Apache/Apache/bin/apachectl $1" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
   fi
done


########################################
# Housekeeping
########################################
echo "\n===> Housekeeping...\n" 2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
chown oracle:oinstall $LOGDIR/$SCRIPTNAME.* > /dev/null


########################################
# End
######################################## 
DATE_END=`date`
echo "\n\n"
echo "===> $SCRIPTNAME Ended"           2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
echo "     Action:        $1"           2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
echo "     Start Time:    $DATE_START"  2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
echo "     End Time:      $DATE_END"    2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
echo "     Log Files Dir: $LOGDIR\n\n"  2>&1 | tee -a $LOGDIR/$SCRIPTNAME.$1.log
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top