Check for DB status and startup DB

EXPERTS,
I need a UNIX shell script to check the DBs status (Running/Shutdown) in the server.If its Running then no issue but if it is in shutdown state ,it should prompt like " DB is shut down you want to startup??" if i enter YES it should startup.
ORACLE_SID are present in /var/opt/oracle/oratab

Atleast need a logic to implement this..

What have you tried till now?

check_stat=`ps -ef|grep ${ORACLE_SID}|grep pmon|wc -l`;
oracle_num=`expr $check_stat`
if [ $oracle_num -lt 1 ]
then
exit 0
fi

i tried this.. but the problem is ORACLE_SID is not set. the list of instances can be found in /var/opt/oracle/oratab.. i dont know how to proceed next..

Could you please how exactly the entries are there in oratab file? usually it will have ORACLE_SID:path at which oracle installed:shoudl it use db start or not (Y/N).

If you have multiple entries then you should know which SID you are searching for..

ya there are multiple entries..
the problem is all of the below mentioned DBs will run on that server.if any one of DB goes down,i need to start up that particular DB.

ZBC102DI:/ora/product/v910:Y
ZBC112DI:/ora/product/v910:Y
ZBC122DI:/ora/product/v910:Y
ZBC132DI:/ora/product/v910:Y
#BUILDDB:/ora/product/v910:N
OMAGENT:/ora/product/omagent10g/agent10g:Y
# *:/ora/product/v910:N
# *:/ora/product/v910:N
*:/ora/product/v910:N

Do you know which ORACLE_SID you wanna serach on the server?

the problem is all of the below mentioned DBs will run on that server.if any one of DB goes down,i need to start up that particular DB.

What method do you use to startup the DB? do you use any scripts (in case of dataguard installed) or you log on to SQL as sysdba and run startup?

---------- Post updated at 02:21 PM ---------- Previous update was at 02:13 PM ----------

You can use this..

 
for line in `awk -F"[#:*]" 'NF==3&&$1~/./{print $1}' /wload/clht/app/etc/oratab` ; do
ps -ef|grep $line|grep pmon  > /dev/null
if [ $? -eq 0 ] ; then
echo "DB $line is up and running"
else
echo "DB $line is not running"
#Your DB startup script goes in here I would say set ORACLE_SID to $line before you startup
fi
done

thanks for the script
when i execute this i am getting nothing,
can u please explain the awk part?

for line in `awk -F"[#:*]" 'NF==3&&$1~/./{print $1}' /var/opt/oracle/oratab` ; do
ps -ef|grep $line|grep pmon > /dev/null
if [ $? -eq 0 ] ; then
echo "DB $line is up and running"
else
echo "DB $line is not running"
fi
done

FYI,i ll connect as sysdba then i ll start the DB

Can you run the script as

 sh -x script_name 

and post the o/p?

awk -F[#:*] NF==3&&$1~/./{print $1} /var/opt/oracle/oratab

This doesnt look like what i asked for :slight_smile:

what u want to know

host $ sh -x test.ksh
+ awk -F[#:*] NF==3&&$1~/./{print $1} /var/opt/oracle/oratab

I am puzzled as per your entries in oratab file it should work :confused:
If you run that awk on command prompt is it returning any result or just blank?

blank :frowning:

then please post the actual (full) content of oratab file..

As per your sample input it should work..

 
home/lscpvbf > cat vv
ZBC102DI:/ora/product/v910:Y
ZBC112DI:/ora/product/v910:Y
ZBC122DI:/ora/product/v910:Y
ZBC132DI:/ora/product/v910:Y
#BUILDDB:/ora/product/v910:N
OMAGENT:/ora/product/omagent10g/agent10g:Y
# *:/ora/product/v910:N
# *:/ora/product/v910:N
*:/ora/product/v910:N
home/lscpvbf > awk -F"[#:*]" 'NF==3&&$1~/./{print $1}' vv
ZBC102DI
ZBC112DI
ZBC122DI
ZBC132DI
OMAGENT
home/lscpvbf >

Actually i am running the script from different location
say /app/test...

the oratab file is the exactly same as the mentioned

It doesnt matter from where u are running script as long as you have provided the full path of the file in the script.

navsan, maybe your awk is behaving different than vidyadhars. Here's another possible solution:

OLDIFS=$IFS
IFS=:
grep -ve '^[#*]' /var/opt/oracle/oratab |while read ORACLE_SID ORACLE_HOME AUTO_START; do
   ps -ef |grep $ORACLE_SID |grep pmon >/dev/null
   if [ $? -eq 0 ]; then
      echo "DB $ORACLE_SID is up and running"
   else
      echo "DB $ORACLE_SID is down. Starting $ORACLE_SID ..."
      $ORACLE_HOME/bin/sqlplus -s / as sysdba <<<startup;
   fi
done
IFS=$OLDIFS