#!/bin/ksh
#============================================================================
# File:		wakepmon.sh
# Date:		21-May-99
# Author:	TGorman
# Description:	UNIX shell script to enable force the PMON process to
#		"wake up" and perform it's duties, using the ORADEBUG WAKEUP
#		command.
#
#		This script should either be SETUID to UNIX account "oracle"
#		and UNIX group "dba" in order to allow people to execute
#		"svrmgrl", or it should be run only under the UNIX account
#		"oracle".  This is due to the fact that it calls Server
#		Mangler...
#
# Modifications:
#
#============================================================================
#
#----------------------------------------------------------------------------
# ...validate command-line parameters...
#----------------------------------------------------------------------------
if (( $# > 0 ))
then
	echo "Usage: \"wakepmon.sh\"; aborting..."
	exit 1
fi
#
#----------------------------------------------------------------------------
# ...verify that ORACLE_HOME environment variable is set...
#----------------------------------------------------------------------------
if [[ "${ORACLE_HOME}" = "" ]]
then
	echo "ORACLE_HOME not set; aborting..."
	exit 1
fi
#
#----------------------------------------------------------------------------
# ...verify that ORACLE_SID environment variable is set...
#----------------------------------------------------------------------------
if [[ "${ORACLE_SID}" = "" ]]
then
	echo "ORACLE_SID not set; aborting..."
	exit 1
fi
#
#----------------------------------------------------------------------------
# ...verify that ORACLE_HOME environment variable appears to be set correctly
#----------------------------------------------------------------------------
if [ ! -d ${ORACLE_HOME}/bin ]
then
	echo "Directory \"${ORACLE_HOME}/bin\" not found; aborting..."
	exit 1
fi
#
#----------------------------------------------------------------------------
# ...verify that the "svrmgrl" executable is present and executable...
#----------------------------------------------------------------------------
if [ ! -x $ORACLE_HOME/bin/svrmgrl ]
then
	echo "\"${ORACLE_HOME}/bin/svrmgrl\" not found; aborting..."
	exit 1
fi
#
#----------------------------------------------------------------------------
# ...verify that the Oracle instance indicated by ORACLE_SID is up and
# running...
#----------------------------------------------------------------------------
_OSPID=`/bin/ps -eaf | \
	grep ora_pmon_${ORACLE_SID} | \
	grep -v grep | \
	awk '{print "~"$2"~"}'`
if [[ "${_OSPID}" = "" ]]
then
	echo "Oracle instance \"${ORACLE_SID}\" not running; aborting..."
	exit 1
fi
#
#----------------------------------------------------------------------------
# ...extract the Oracle PID from V$PROCESS for PMON...
#----------------------------------------------------------------------------
_TmpFile=/tmp/wakepmon.$$
$ORACLE_HOME/bin/svrmgrl << __EOF__ > ${_TmpFile}
connect internal
select 'ORAPID=', pid from v\$process where program like '%(PMON)%';
exit
__EOF__
if (( $? != 0 ))
then
	echo "\n\"svrmgrl\" exited with failure status; aborting...\n"
	exit 1
fi
#
#----------------------------------------------------------------------------
# ...extract the PMON process ID from the temporary spool file...
#----------------------------------------------------------------------------
integer _ORAPID=`grep "ORAPID=" ${_TmpFile} | awk '{print $2}'`
#
#----------------------------------------------------------------------------
# ...call the ORADEBUG WAKEUP command for the PMON process...
#----------------------------------------------------------------------------
$ORACLE_HOME/bin/svrmgrl << __EOF__ >> ${_TmpFile}
connect internal
oradebug wakeup ${_ORAPID}
exit
__EOF__
if (( $? != 0 ))
then
	echo "\n\"svrmgrl\" exited with failure status; aborting..."
	echo "Check log file \"${_TmpFile}\" for more info...\n"
	exit 1
fi
#
echo "\n\"Wakeup\" command issued to PMON successfully...\n"
rm -f ${_TmpFile}
exit 0

