3
# Copyright 2003-2004, 2006-2011 Red Hat, Inc.
6
# Hardy Merrill <hmerrill at redhat.com>
7
# Lon Hohberger <lhh at redhat.com>
8
# Michael Moon <Michael dot Moon at oracle.com>
10
# This program is Open Source software. You may modify and/or redistribute
11
# it persuant to the terms of the Open Software License version 2.1, which
12
# is available from the following URL and is included herein by reference:
14
# http://opensource.org/licenses/osl-2.1.php
16
# chkconfig: 345 99 01
17
# description: Service script for starting/stopping \
18
# Oracle(R) Database 10g on \
19
# Red Hat Enterprise Linux 5
23
# (1) You can comment out the LOCKFILE declaration below. This will prevent
24
# the need for this script to access anything outside of the ORACLE_HOME
27
# (2) You MUST customize ORACLE_USER, ORACLE_HOME, ORACLE_SID, and
28
# ORACLE_HOSTNAME to match your installation if not running from within
31
# (3) Do NOT place this script in shared storage; place it in ORACLE_USER's
32
# home directory in non-clustered environments and /usr/share/cluster
33
# in rgmanager/Red Hat cluster environments.
35
# Oracle is a registered trademark of Oracle Corporation.
36
# Oracle9i is a trademark of Oracle Corporation.
37
# Oracle10g is a trademark of Oracle Corporation.
38
# All other trademarks are property of their respective owners.
41
# $Id: orainstance.sh 127 2009-08-21 09:17:52Z hevirtan $
43
# Original version is distributed with RHCS. The modifications include
44
# the following minor changes:
45
# - Meta-data moved to a dedicated file
46
# - Support for multiple listeners
48
# - SysV init support removed. Only usable with rgmanager
51
. /etc/init.d/functions
53
declare SCRIPT="`basename $0`"
54
declare SCRIPTDIR="`dirname $0`"
56
# Required parameters from rgmanager
57
ORACLE_USER=$OCF_RESKEY_user
58
ORACLE_HOME=$OCF_RESKEY_home
59
ORACLE_SID=$OCF_RESKEY_name
61
# Optional parameters with default values
62
LISTENERS=$OCF_RESKEY_listeners
63
LOCKFILE="/tmp/.oracle10g-${ORACLE_SID}.lock"
64
[ -n "$OCF_RESKEY_lockfile" ] && LOCKFILE=$OCF_RESKEY_lockfile
66
export LISTENERS ORACLE_USER ORACLE_HOME ORACLE_SID LOCKFILE
67
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
68
export PATH=$ORACLE_HOME/bin:$PATH
70
declare -i RESTART_RETRIES=3
71
declare -r DB_PROCNAMES="pmon"
72
declare -r LSNR_PROCNAME="tnslsnr"
76
# Start Oracle (database portion)
83
tmpfile=/tmp/$SCRIPT-start.$$
84
logfile=/tmp/$SCRIPT-start.log
86
# Set up our sqlplus script. Basically, we're trying to
87
# capture output in the hopes that it's useful in the case
88
# that something doesn't work properly.
89
echo "startup" > $tmpfile
90
echo "quit" >> $tmpfile
92
sqlplus "/ as sysdba" < $tmpfile > $logfile
97
# Dump logfile to /var/log/messages
98
initlog -q -c "cat $logfile"
100
if [ $rv -ne 0 ]; then
102
initlog -n $SCRIPT -q -s "sqlplus returned 1, failed"
107
# ORA-.....: failure, we failed
108
grep -q "failure" $logfile
112
if [ $rv -eq 0 ]; then
113
initlog -n $SCRIPT -q -s "found failure in stdout, returning 1"
122
# Stop Oracle (database portion)
129
tmpfile=/tmp/$SCRIPT-stop.$$
130
logfile=/tmp/$SCRIPT-stop.log
132
ora_procname="ora_${DB_PROCNAMES}_${ORACLE_SID}"
134
if [ $? -ne 0 ]; then
135
# No pmon process found, db already down
140
echo "shutdown immediate" > $tmpfile
141
echo "quit" >> $tmpfile
143
sqlplus "/ as sysdba" < $tmpfile > $logfile
148
# Dump logfile to /var/log/messages
149
initlog -q -c "cat $logfile"
151
# sqlplus returned failure. We'll return failed to rhcs
152
if [ $rv -ne 0 ]; then
154
initlog -n $SCRIPT -q -s "sqlplus returned 1, failed"
158
grep -q failure $logfile
162
# If we see 'failure' in the log, we're done.
163
if [ $rv -eq 0 ]; then
164
initlog -n $SCRIPT -q -s "found failure in stdout, returning 1"
173
# Destroy any remaining processes with refs to $ORACLE_SID
179
pids=`ps ax | grep $ORACLE_SID | grep -v grep | awk '{print $1}'`
181
initlog -n $SCRIPT -s "<err> Not all Oracle processes exited cleanly, killing"
185
if [ $? -eq 0 ]; then
186
initlog -n $SCRIPT -s "Killed $pid"
195
# Wait for oracle processes to exit. Time out after 60 seconds
200
while ps ax | grep $ORACLE_SID | grep -q -v $LSNR_PROCNAME | grep -q -v grep; do
201
if [ $n -ge 90 ]; then
213
# Get database background process status. Restart it if it failed and
214
# we have seen the lock file.
217
declare -i subsys_lock=$1
222
for procname in $DB_PROCNAMES ; do
223
ora_procname="ora_${procname}_${ORACLE_SID}"
226
if [ $? -eq 0 ] ; then
227
# This one's okay; go to the next one.
231
# We're not supposed to be running, and we are,
232
# in fact, not running...
233
if [ $subsys_lock -ne 0 ]; then
237
for (( i=$RESTART_RETRIES ; i; i-- )) ; do
238
# this db process is down - stop and
239
# (re)start all ora_XXXX_$ORACLE_SID processes
240
initlog -q -n $SCRIPT -s "Restarting Oracle Database..."
244
if [ $? == 0 ] ; then
245
# ora_XXXX_$ORACLE_SID processes started
246
# successfully, so break out of the
247
# stop/start # 'for' loop
252
if [ $i -eq 0 ]; then
253
# stop/start's failed - return 1 (failure)
254
initlog -q -n $SCRIPT -s "Restart failed, retuning 1"
263
# Get the status of the Oracle listener process
266
declare -i subsys_lock=$1
268
declare -r LISTENER=$3
270
lsnrctl status $LISTENER >& /dev/null
272
if [ $rv == 0 ] ; then
273
return 0 # Listener is running fine
276
# We're not supposed to be running, and we are,
277
# in fact, not running. Return 3
278
if [ $subsys_lock -ne 0 ]; then
282
# Listener is NOT running (but should be) - try to restart
283
for (( i=$RESTART_RETRIES ; i; i-- )) ; do
284
initlog -n $SCRIPT -q -s "Restarting Oracle listener ($LISTENER)"
285
lsnrctl start $LISTENER
286
lsnrctl status $LISTENER >& /dev/null
287
if [ $? == 0 ] ; then
288
break # Listener was (re)started and is running fine
292
if [ $i -eq 0 ]; then
293
# stop/start's failed - return 1 (failure)
294
initlog -n $SCRIPT -q -s "Listener restart failed, retuning 1"
298
lsnrctl status $LISTENER >& /dev/null
299
if [ $? != 0 ] ; then
300
initlog -n $SCRIPT -q -s "Listener status failed, retuning 1"
301
return 1 # Problem restarting the Listener
303
return 0 # Success restarting the Listener
308
# Helps us keep a running status so we know what our ultimate return
309
# code will be. Returns 1 if the $1 and $2 are not equivalent, otherwise
310
# returns $1. The return code is meant to be the next $1 when this is
311
# called, so, for example:
313
# update_status 0 <-- returns 0
314
# update_status $? 0 <-- returns 0
315
# update_status $? 3 <-- returns 1 (values different - error condition)
316
# update_status $? 1 <-- returns 1 (same, but happen to be error state!)
319
# update_status $? 3 <-- returns 3
324
declare -i old_status=$1
325
declare -i new_status=$2
331
if [ $old_status -ne $new_status ]; then
332
initlog -n $SCRIPT -q -s "$old_status vs $new_status - returning 1"
341
# Print an error message to the user and exit.
344
#echo "Please configure this script ($0) to"
345
#echo "match your installation."
347
#echo " $1 failed validation checks."
348
initlog -n $SCRIPT -q -s "$1 failed validation checks"
354
# Do some validation on the user-configurable stuff at the beginning of the
357
validation_checks() {
358
# If the oracle user doesn't exist, we're done.
359
[ -n "$ORACLE_USER" ] || oops "ORACLE_USER"
360
id -u $ORACLE_USER > /dev/null || oops "ORACLE_USER"
361
id -g $ORACLE_USER > /dev/null || oops "ORACLE_USER"
363
# If the oracle home isn't a directory, we're done
364
[ -n "$ORACLE_HOME" ] || oops ORACLE_HOME
366
# If the oracle SID is NULL, we're done
367
[ -n "$ORACLE_SID" ] || oops ORACLE_SID
369
# Super user? Automatically change UID and exec as oracle user.
370
# Oracle needs to be run as the Oracle user, not root!
371
if [ "`id -u`" = "0" ]; then
372
su $ORACLE_USER -c "$0 $*"
376
# If we're not root and not the Oracle user, we're done.
377
[ "`id -u`" = "`id -u $ORACLE_USER`" ] || exit 1
378
[ "`id -g`" = "`id -g $ORACLE_USER`" ] || exit 1
391
initlog -n $SCRIPT -q -s "Starting Oracle Database"
394
for LISTENER in ${LISTENERS}; do
395
logfile=/tmp/$SCRIPT-lsn-$$.log
396
initlog -n $SCRIPT -q -s "Starting Oracle Listener $LISTENER"
397
lsnrctl start $LISTENER > $logfile
398
initlog -q -c "cat $logfile"
402
if [ -n "$LOCKFILE" ]; then
413
if ! [ -e "$ORACLE_HOME/bin/lsnrctl" ]; then
414
initlog -n $SCRIPT -q -s "Oracle Listener Control is not available ($ORACLE_HOME not mounted?)"
418
initlog -n $SCRIPT -q -s "Stopping Oracle Database"
422
for LISTENER in ${LISTENERS}; do
423
initlog -n $SCRIPT -q -s "Stopping Oracle Listener $LISTENER"
424
lsnrctl stop $LISTENER
427
initlog -n $SCRIPT -q -s "Waiting for all Oracle processes to exit"
430
if [ $? -ne 0 ]; then
431
initlog -n $SCRIPT -q -s "WARNING: Not all Oracle processes exited cleanly"
434
if [ -n "$LOCKFILE" ]; then
442
# Find and display the status of iAS infrastructure.
444
# This has three parts:
445
# (1) Oracle database itself
446
# (2) Oracle listener process
447
# (3) OPMN and OPMN-managed processes
449
# - If all are (cleanly) down, we return 3. In order for this to happen,
450
# $LOCKFILE must not exist. In this case, we try and restart certain parts
451
# of the service - as this may be running in a clustered environment.
453
# - If some but not all are running (and, if $LOCKFILE exists, we could not
454
# restart the failed portions), we return 1 (ERROR)
456
# - If all are running, return 0. In the "all-running" case, we recreate
457
# $LOCKFILE if it does not exist.
460
declare -i subsys_lock=1
464
# Check for lock file. Crude and rudimentary, but it works
465
if [ -z "$LOCKFILE" ] || [ -f $LOCKFILE ]; then
469
# Check database status
470
get_db_status $subsys_lock $depth
471
update_status $? # Start
474
# Check & report listener status
475
for LISTENER in ${LISTENERS}; do
476
get_lsnr_status $subsys_lock $depth $LISTENER
477
update_status $? $last
481
# No lock file, but everything's running. Put the lock
482
# file back. XXX - this kosher?
483
if [ $last -eq 0 ] && [ $subsys_lock -ne 0 ]; then
491
########################
492
# Do some real work... #
493
########################
497
cat `echo $0 | sed 's/^\(.*\)\.sh$/\1.metadata/'`
512
status_oracle $OCF_CHECK_LEVEL
521
echo "usage: $SCRIPT {start|stop|restart|status|monitor|meta-data}"