Check the connectivity of the DB through script, exit if no connection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check the connectivity of the DB through script, exit if no connection
# 1  
Old 11-01-2012
Check the connectivity of the DB through script, exit if no connection

check the connectivity of the DBs through script, script should exit if no connection and display the output as below.

connectivity for DB1 is OK
connectivity for DB2 is OK
connectivity for DB3 is FAILED






Code:
 
 
for DB in 1 2 3 
do

(sqlplus -s ${USERNAME}/${PASSWORD}@${DB}  << EOF
whenever sqlerror exit  255 rollback;
WHENEVER SQLERROR EXIT SQL.SQLCODE ;
quit;
EOF
)
if [ $? -eq 0 ]
then
echo " connectivity for ${DB} is OK"
else
echo " Connection failed for ${DB} "
fi
sleep 1
done

please help me ..............................................
# 2  
Old 11-01-2012
You can use below code to test connectivity:-

Code:
Out=`sqlplus -s ${USERNAME}/${PASSWORD}@${DB} << EOF
set echo off head off feed off pagesize 0 trimspool on linesize 1000
select 'OK' from dual;
exit;
EOF`

if [ "$Out" = "OK" ]
then
        echo "Connectivity OK"
else
        echo "Connectivity FAILED"
fi

# 3  
Old 11-01-2012
thanks bipinagith....................onw quick question, out of 3 DBs, DB2 has prblem with connection, will it display the output as below

DB1 OK
DB2 FAILED
DB3 OK
# 4  
Old 11-01-2012
Yes indeed, since connection fails and the Out variable doesn't have the value OK in it, you will get the expected message.
# 5  
Old 11-02-2012
when it is trying to connect to DB2 , but it is taking more but output is not displaying anything................and it is not moving to DB3


Code:
 
for DB in 1 2 3 4 5
do
var=`sqlplus -s ${USERNAME}/${PASSWORD}@${dbname} << EOF
set echo off;
set head off;
set feedback off;
set pagesize 0;
set trimspool on ;
select 'OK'  from dual;
exit;
EOF`
if [ "$var" = "OK" ]
then
echo " connectivity for $dbname is OK.............."
else
echo " Connection for $dbname is FAILED............... "
fi

output is :

Code:
 
connectivity for DB1 is OK..............

but not moving to DB3 DB4 DB5 ???
please help
# 6  
Old 11-02-2012
Try connecting to DB2 outside the script, if you are still not getting any response you should contact your DBA for any assistance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Script to check the FTP connection

Hi, I need a script which check the FTP connections, if its working fine then no issue. Incase not working fine then Trigger the mail. Please help on this. Thanks (4 Replies)
Discussion started by: Keshav Kalra
4 Replies

2. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

3. Shell Programming and Scripting

Help with shell script to check the tcp network connectivity between server

Hello, I have a requirement to check the tcp network connectivity between server it's running on and the list of host's and ports combination. i have written the below code but it doesn't work, but when i execute the nc command outside the script it works fine. please help me where i am... (8 Replies)
Discussion started by: sknovice
8 Replies

4. Solaris

Sybase Connectivity Check through Shell Script

Hi, I need to check the sysbase database connectivity through the Unix Shell Script. Can you help me on the how to write the script to test the sysbase database connection. Thanks in Advance Nandha (0 Replies)
Discussion started by: nandha2387
0 Replies

5. Shell Programming and Scripting

Check connectivity script

This past weekend I had some issues with my ISP. So for future purpose I'm going to have some logging on my internet so I'm able to attach log files to my complaint email if this issue reoccurs. Decided to do a simple ping script that runs every 5 or 10 min with crontab if ping fail write date... (5 Replies)
Discussion started by: chipmunken
5 Replies

6. Shell Programming and Scripting

Check file and if it doesnt exist , exit script

Hi, Another problem, here is my code #!/bin/sh dir='/opt/apps/script/CSV' datadir='/opt/apps/script/data' while : ; do ls -1rt $dir/*.csv > /dev/null 2>&1 if ;then cp $datadir/weekly.txt $dir/weekly.csv else exit 0 fi done (10 Replies)
Discussion started by: tententen
10 Replies

7. Shell Programming and Scripting

Linux: Writing a tricky script to check connectivity

So, first and foremost, I'm having issues with my internet connection. Periodically, the connection drops across the network. The fix is simple enough: restart the modem. However, this gets old when the connection dies out every hour. I can hit my surfboard on 192.168.100.1, and navigate to a... (5 Replies)
Discussion started by: kungfujoe
5 Replies

8. Shell Programming and Scripting

Script to check connectivity

I want to write a script to check if a unix box say abc.tdc.cin.net can be connected or not on certain port say 22. right know i have to telnet them manually from DOS prompt and if it is successful then isay it is connected. Also to check Database connectivity I am using tnsping From DOS prompt.... (3 Replies)
Discussion started by: kukretiabhi13
3 Replies

9. Shell Programming and Scripting

check exit status - Expect Script

from my main script, i am calling an expect script. there are a lot of conditions in the Expect script and it can have any exit value based on success or failure of the Expect Script. how can i check the exit status of Expect scritp in the main script. (1 Reply)
Discussion started by: iamcool
1 Replies

10. UNIX for Dummies Questions & Answers

how to check exit status in awk script

Hi, I have a main program which have below lines - awk -f test.awk inputFileName - I wonder how to check status return from awk script. content of awk script: test.awk --- if ( pass validation ) { exit 1 } else { (1 Reply)
Discussion started by: epall
1 Replies
Login or Register to Ask a Question