Solved

Checking if database is open via ksh script

Posted on 2013-06-26
11
782 Views
Last Modified: 2013-07-06
I have the following code to check if database is up and running and it works in most cases but sometimes I get an error
"ORA-01219: database not open: queries allowed on fixed tables/views only".

ps -ef|grep -v grep| grep $ORACLE_SID | grep ora_pmon > /dev/null
        if (( $? == 0 )) # is the pmon process running zero is yes
        then
                #check if sqlplus is installed
                if [ -f $ORACLE_HOME/bin/sqlplus ]
                then
                        sqlplus -s "/ as sysdba" <<! >> $FILE1
                        set lines 132
                        set wrap off
			select * from dba_data_files;
                        exit;

!

                fi
        fi
 

Open in new window



If I just run select name from v$database;, it returns the result without an issue but does not work on dba_data_files and other similar tables and views.

 I also tried to manually loggin in to the database and ran
SQL> select * from dba_data_files;
select * from dba_data_files
              *
ERROR at line 1:
ORA-01219: database not open: queries allowed on fixed tables/views only.

The same issue also happens when checking a standby database in a Dataguard configuration.

But gotten the same result. Is there any way to check if database is open or if it is a standby database, using ksh script?
0
Comment
Question by:YZlat
  • 4
  • 3
11 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39278020
To see if it is open:
select status from v$instance;

http://docs.oracle.com/cd/E11882_01/server.112/e25513/dynviews_2002.htm#REFRN30105


Standby status:
select database_role from v$database;

http://docs.oracle.com/cd/E11882_01/server.112/e25513/dynviews_1096.htm#REFRN30047
0
 
LVL 35

Author Comment

by:YZlat
ID: 39278163
What's the best way to encorporate this into my script? Is there any easier way scriptwise?
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 300 total points
ID: 39278201
It would require another sqlplus launch but run those selects and read the results into a local variable.

There are several examples out there on how to do this.

For example:
http://asktom.oracle.com/pls/asktom/f/f?p=100:11:0::::P11_QUESTION_ID:430819636473
http://sureshvaishya.blogspot.com/2008/10/store-sqlplus-output-to-variable-in.html

>>Is there any easier way scriptwise?

Possibly capture the results and interpret the oracle error messages.
0
 
LVL 23

Expert Comment

by:David
ID: 39278430
Variation on the theme, skip the old script/cron approach in favor of Grid or Cloud monitoring.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Author Comment

by:YZlat
ID: 39278509
I tried the following code:

db_status=`sqlplus -s "/ as sysdba" <<EOF
                                set heading off feedback off verify off
                        select status from v\$instance;
                        exit
EOF
`
echo $db_status

Open in new window


but got a lot of junk in there
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39278517
From a command prompt what are you getting with:
sqlplus -s "/ as sysdba" <<EOF
                                set heading off feedback off verify off
                        select status from v\$instance;
                        exit
EOF
0
 
LVL 35

Author Comment

by:YZlat
ID: 39278532
figured it out!  I neded to use v\\$instance instead of v\$instance and then it worked
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39278541
Mind if I ask why the "B" penalty grade?

What additional information did you need before providing an "A" grade?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now