How to connect to a DB installed on another host using shell script?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to connect to a DB installed on another host using shell script?
# 1  
Old 02-15-2019
How to connect to a DB installed on another host using shell script?

Hi Experts

There are two hosts say A and B. On host B, Oracle DB is installed and running. I am trying to check if the DB on host B is up and running fine from a script on host A. Is this possible. I tried using commands like sql plus but those doesn't seem to help. Could you please guide.
# 2  
Old 02-15-2019
IMHO you need to see 2 things running on a server to consider an oracle instance is up, and perhaps running:
1) the listener
2) a oracle pmon process associated to that instance
Using sqlplus wont work if no listener to start with...

So a shell script would be to connect to the host B and using ps command and the adequate grep to return the wanted information
and your shell would be using a Here doc formating
# 3  
Old 02-15-2019
Thank you vbe for the reply. This is what i have written. Could you please see if this is going to work:

Script on Host B:

Code:
#Variables
rval=0

#Check if DB is up
ps -ef|grep -v grep|grep pmon > /dev/null
rval=$?
if (( $rval == 0 ))
then
   echo "DB is up and running."
else
    echo "Waiting for DB to start"
fi

while [ "$rval" != "0" ]
 do
    echo " Trying again after 10 seconds"
    sleep 10
ps -ef|grep -v grep|grep pmon > /dev/null
rval=$?
echo "DB is up and running."
done


Script on Host A:

Code:
#Variables
rval1=0
local checkDB
checkDB=$(ssh user@server /scripts/checkDB.sh)
echo $?
if (( $rval == 0 ))
then
ps -ef|grep -v grep|grep server.sh > /dev/null
rval1=$?
if (( $rval1 == 0 ))
then
   echo "Server is already running."
else
   echo "Starting Server ..."
   $home/bin/startup.sh
   ps -ef|grep -v grep|grep server.sh > /dev/null
   rval1=$?
   if (( $rval1 == 0 ))
   then
      echo "Server is starting."
   fi
fi
 exit $rval



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-15-2019 at 07:10 AM.. Reason: Added CODE tags.
# 4  
Old 02-15-2019
Thanks for posting this. What error messages/output do you actually get? Please post them (in CODE tags for clarity) and we can try to help. Does it say sqlplus: not found or connection refused, something about credentials or what? It could be many things at present.

If there is not useful output because your script covers it up, try running your script with -x set. You don't mention a shell, but it might work as one of these or a similar variation:-
Code:
ksh -x /path/to/script
bash -x /path/to/script

Indenting your code will help clarity and show you more easily where conditions and loops are effective.

Your "script on server A" also has this statement if (( $rval == 0 )) yet you do not set this. Your previous active statement may generate a return code or some output, but you don't use them. The return code from the script on Server B will not be passed back to server A. Server A will see the return code of the ssh command itself, not what was run by it so that will just tell you if ssh connected.


Have you considered tnsping to determine database state?



Thanks, in advance,
Robin

Last edited by rbatte1; 02-15-2019 at 07:22 AM..
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 02-15-2019
I'm afraid that none of the scripts posted will do what you expect. On the other hand, it's not too well known what your targets are. Some comments:

Host B:
- is that script's path /scripts/checkDB.sh?
- it will output lines after lines "Trying ..." and "DB is up ..." if the pmon test fails. This is important for the planned evaluation on host A.
- try ps -ef | grep -q [p]mon in lieu of your lenghtier approach.

Host A:
- the local variable checkDB will be filled with text (at least "DB is up ... if not with lines after lines) but never evaluated.
- rval is set to 0 and not modified before the first zero test.
- what is the process test for server.sh for?
- why do you run startup.sh on host A if the DB should run on host B?
- same comment on ps ... | grep ... as for host B.
# 6  
Old 02-15-2019
Thank you for getting back. I am trying to bring up a application server on Host A if the DB is up on host B. tnsping and sqlplus are not working when trying to execute on Host A. So i thought of doing an ssh to host B and if the process ID is up or retry till the process ID on host B is up and then start the application server on Host A.
# 7  
Old 02-15-2019
So, given that my crystal ball is in for servicing at the moment, when you say "not working", can you actually show us the errors? Without them I might suggest:-
  • You don't have the Oracle client installed
  • Your network configuration is wrong 'somewhere'
  • tnsnames.ora is incomplete
  • The server is not listening on the defined port
  • You have a spelling mistake
  • Is the power on even?
It might seem a little flippant and I apologise, but if we cannot see the symptoms, we can't help to diagnose it.



Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to connect

hello i try to made a report to conect all database to diferent OS HP-UX/LINUX/SOLARIS/AIX this is my example i have 5 db OS are HP-UX ps -fea | grep pmon root 1120 1 0 Nov 29 ? 5:14 ipmon -sD oracle 10286 1 0 Nov 29 ? 27:19 ora_pmon_BD1 oracle 10431... (7 Replies)
Discussion started by: ceciaide
7 Replies

2. Shell Programming and Scripting

Connect to target host from Source host.

Hi All, Need to connect to target host and execute a command and connect back to source host to continue with next set of command execution. Is there a utility/command using which we can connect to target host ? Please suggest. Note: Netezza database is instaled on Linux server. ... (4 Replies)
Discussion started by: Nagaraja Akkiva
4 Replies

3. Shell Programming and Scripting

CURL getting Error- couldn't connect to host

Hi i am trying to open HTTP by curl command. i am tryng to do that from command or script BASH. also iptables is off it doesnt work i am getting an error: curl: (7) coudn't connect to host the scipt is: #! /bin/bash curl http://www.cnn.com (1 Reply)
Discussion started by: zigizag
1 Replies

4. Shell Programming and Scripting

Portable Shell Script - Determine Which Version of Binary is Installed?

I currently have a shell script that utilizes the "Date" binary - this application is slightly different on OS X (BSD General Commmand) and Linux systems (gnu date). In particular, the version on OS X requires the following to get a date 14 days in the future "date -v+14d -u +%Y-%m-%d" where gnu... (1 Reply)
Discussion started by: colinjohnson
1 Replies

5. Shell Programming and Scripting

Connect to Oracle in Windows XP from Linux HOST

Hi everyone, I have oracle 9i installed in Windows XP and i have ubuntu in VM WARE. I would like to know how to connect to Oracle in windows xp from Linux through shel scripting...... (3 Replies)
Discussion started by: Zimbu
3 Replies

6. Red Hat

Installed apache server , can't connect from outside (using CentOS in WMware )

Hello all I installed apache in CentOS 5.5 ,after searching the web for tips on configuration I did the fallowing stuff to unable connecting the http server from outside. In /etc/httpd/conf/httpd.conf I changed the Listen value to 0.0.0.8011 Then checked with then check with: netstat -anp and I... (2 Replies)
Discussion started by: umen
2 Replies

7. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

8. Shell Programming and Scripting

Run a shell script from one host which connext to remote host and run the commands

I want to write a script which would run from one host say A and connect to other remote host B and then run rest of commands in that host. I tried connecting from A host to B with SSH but after connecting to host B it just getting me inside Host B command prompt. Rest of the script is not running... (6 Replies)
Discussion started by: SN2009
6 Replies

9. Shell Programming and Scripting

How can my unix shell script automatically connect to remote host?

How can my unix shell script automatically connect to remote host? Assume that there is a remote host called "rhost". When I connect to that host i give the command "telnet rhost". It then asks me for my id and password. Once i give it connects there. I want to automate these steps. I want... (8 Replies)
Discussion started by: digdarshan
8 Replies

10. UNIX for Advanced & Expert Users

sendmail : connect local host (brocken)

hi there I've got a strange problem with a webserver box (suse linux 7.2). My MTA is sendmail, and when I tried manually send a mail. Sendmail -v "user@something" < /etc/hosts The Server tried "simon@localhost... Connecting to local..." but nothing happend and the system hold on. This... (3 Replies)
Discussion started by: mrsaint
3 Replies
Login or Register to Ask a Question