Press "Enter" to skip to content

Simple Shell Script Example To Check If Oracle Database Is Up In Linux / Unix

Check Oracle Database status in bash shell

You want to write a simple shell script in Linux / Unix to check whether Oracle Database is Up or not. The below is the example script which you can write using vi editor. Follow these steps to create and execute a shell script in Linux / Unix.

Run vi editor and add below content by changing values accordingly as mentioned in next paragraph.

#! /bin/bash
ORACLE_SID=FOXDEV
ORACLE_HOME=/orahome1/oracle/product/11.2.0/db_1
PATH=$ORACLE_HOME/bin:$PATH
echo “select ‘Database is UP and Running!’ db_status from dual;” | sqlplus -s system/psw
exit 0

Change the ORACLE_SID and ORACLE_HOME variable values to your Oracle database information and change the password also for system user or you can use any other user credential details as you are executing just a simple query from dual. After completing the editing save this file and give a name, for this example we can assume file name as isdbup.sh.

Now to execute the script “isdbup.sh” first you need to make the script executable, so give the following chmod command to make the file executable:

$ chmod u+x isdbup.sh

The above command will change the permission for the owner (u) of the file to executable (x) and now you would be able to execute this script as following:

$ isdbup.sh

db_status
—————
Database is UP and Running!

One Comment

  1. Second Home Yusuf Second Home Yusuf December 22, 2015

    My version

    #!/bin/bash

    source /home/oracle/.bash_profile

    ORACLE_SID=mysid
    export ORACLE_SID
    ORAENV_ASK=NO

    echo "exit" | /oracle/u01/oracle/product/11.2.0.3/dbhome_1/bin/sqlplus -L mysid/[email protected] | grep Connected > /dev/null
    if [ $? -eq 0 ]
    then
    echo "OK – `date`" > /home/oracle/log-check-connection.txt
    else
    echo 'SINTA DB ALERT – Database cannot connect:' `date` | mail -s "DB Alert: Database Connection FAIL!!!" [email protected]il.address
    echo "NOT OK – `date`" > /home/oracle/log-check-connection.txt
    fi

Comments are closed.