Oracle sysV init script on the fly

Every time I need an Oracle init script file, it’s a terrible pain! No way to use a simple start/stop/status (with nagios check)…

Today I write “on-the-fly” this very simple script to run Oracle Database on RedHat Cluster Suite without nagios plugin.

#!/bin/sh
 
#chkconfig: 2345 80 05
#description: Oracle DB
 
ORA_HOME=/opt/oracle/product/9.2.0
ORA_OWNER=oracle
LOG="/dev/null"
 
prog="Oracle"
lockfile="/var/lock/subsys/oracle"
 
# Source function library.
if [ -r "/etc/rc.d/init.d/functions" ]; then
	. /etc/rc.d/init.d/functions
fi
 
# Oralce configurations
if [ -r "/etc/sysconfig/oracle.conf" ]; then
        . /etc/sysconfig/oracle.conf
fi
 
export ORA_HOME
export ORA_OWNER
export LOG
export prog
export lockfile
 
if [ ! -f $ORA_HOME/bin/dbstart ]
then
  echo "Oracle startup: cannot start"
  exit
fi
 
RETVAL=0
 
start() {
     if [ -f ${lockfile} ]; then
         echo "Oracle already running"
         exit 1
     fi
 
     echo -n "Starting $prog: "
     runuser - $ORA_OWNER -c "$ORA_HOME/bin/dbstart"
     RETVAL_DB=$?
     runuser - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start > $LOG"
     RETVAL_LSN=$?
     if (($RETVAL_DB == "0")) && (($RETVAL_LSN == "0")) ; then
         RETVAL="0"
	 touch ${lockfile}
	 echo_success
     else
         RETVAL="1"
	 echo_failure
     fi
     return $RETVAL
}
 
stop() {
     if [ ! -f ${lockfile} ]; then
         echo "Oracle already stopped"
         exit 0
     fi
     echo -n $"Stopping $prog: "
     runuser - $ORA_OWNER -c "$ORA_HOME/bin/dbshut"
     RETVAL_DB=$?
     runuser - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop > $LOG"
     RETVAL_LSN=$?
     if (($RETVAL_DB == "0")) && (($RETVAL_LSN == "0")) ; then
         RETVAL="0"
	 rm -f ${lockfile}
	 echo_success
     else
         RETVAL="1"
	 echo_failure
     fi
     return $RETVAL
}
 
status() {
     RETVAL=1
     pid=`pgrep -u $ORA_OWNER`
     if [ -n "$pid" ]; then
           RETVAL=0
           echo -n "$prog is running..."
           echo_success
     else
           echo -n "$prog is not running."
           echo_failure
     fi
     return $RETVAL
}
 
case "$1" in
  "start")
       start
       ;;
  "stop")
       stop
       ;;
  "restart")
       start
       stop
       ;;
  "status")
       status
       ;;
  *)
       echo $"Usage: $prog {start|stop|restart|status}"
       exit 1
 
esac
echo
exit $RETVAL

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment:

-->