keskiviikko 31. lokakuuta 2012

Simplified automated Oracle database startup script (updated)



Simplified Oracle database startup script

In the previous post I described what are the generic steps of creating a linux service and how to have that service automatically start when the machine boots up. The same post contains the script for automatically starting Oracle DB at server startup.

I have now simplified the script. In the original post of this article I had removed the use of external lock files completely and  but used netstat to check if the listener up or down and ps to check if the database processes are up and about.

This did not completely work as I had originally planned as I noticed later that lock files have deeper meaning with chkconfig. Namely if you do not have the /var/lock/subsys/oracle file the service will not be automatically shut down as the system changes between run levels when the whole server is shut down. So after all you need the lock files in the end. You can see the changed bits in red colour below.

Here is the script in its current format:



#!/bin/bash
#
# Run level script to start Oracle DB services
# --------------------------------------------
# chkconfig: 35 91 18
# description: Oracle DB server

. /etc/init.d/functions

OLD_PATH=$PATH
export ORACLE_SID=orcl
export ORACLE_UNQNAME=$ORACLE_SID
export ORAENV_ASK="NO"
export PATH=/usr/local/bin:$PATH
source /usr/local/bin/oraenv >/dev/null
export PATH=$OLD_PATH

OUSER="oracle"
OPATH=$ORACLE_HOME
OPORT=1521
SRVNM="Oracle DB server"
LOCKD=/var/lock/subsys
LOCKF=$LOCKD/oracle

case "$1" in

  start)
    echo "$SRVNM:"
    if [ -d $LOCKD ]; then
      touch $LOCKF
    fi
    su - $OUSER -c "$OPATH/bin/dbstart $OPATH"
    [ $? -eq 0 ] && success || failure
    ;;

  stop)
    echo "$SRVNM:"
    su - $OUSER -c "$OPATH/bin/dbshut $OPATH"
    if [ -e $LOCKF ]; then
      rm -f $LOCKF
    fi
    [ $? -eq 0 ] && success || failure
    ;;

  restart)
    $0 stop
    $0 start
    ;;

  status)
    echo "$SRVNM:"
    netstat -tln | grep ":$OPORT " > /dev/null
    [ $? -eq 0 ] && echo "  LISTENER is running..." || echo "  LISTENER is stopped"
    [ $(ps -ef | grep "^$OUSER.*ora_.*_$ORACLE_SID\$" | wc -l) -gt 1 ] && \
      echo "  Server is running..." || echo "  Server is stopped"
    ;;
  *)
    echo $"Usage: $0 {start|stop|status}"
    exit 1
esac

exit 0


Note:
If you do not like the shorthand for if-statements in status command part:
[ $? -eq 0 ] && echo " LISTENER is running..." || echo " LISTENER is stopped"
[ $(ps -ef | grep "^$OUSER.*ora_.*_$ORACLE_SID\$" | wc -l) -gt 1 ] && \
echo " Server is running..." || echo " Server is stopped"

You can rewrite those parts in more readable but longer format with if statements like this:

if [ $? -eq 0 ]; then
echo " LISTENER is running..." 

else 
echo " LISTENER is stopped"
fi
if [ $(ps -ef | grep "^$OUSER.*ora_.*_$ORACLE_SID\$" | wc -l) -gt 1 ]; then
echo " Server $ORACLE_SID is running..."
else
echo " Server $ORACLE_SID is stopped"
fi


Ei kommentteja:

Lähetä kommentti