Link to home
Start Free TrialLog in
Avatar of Christian Palacios
Christian PalaciosFlag for Canada

asked on

Oracle SQLPlus Command Remotely Via Script

Hi there,

I'm trying to run a bash script remotely on an Oracle server, but when I do I get errors.  The script runs just fine locally.

Command from server1 to server2:
ssh oracle@10.41.22.69 /home/oracle/juletest2.sh
Error on server 1:
Error 6 initializing SQL*Plus
SP2-0667: Message file sp1<lang>.msb not found
SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory


Same Command on server2:
oracle@VMUSDevORA01 ~]$ ./juletest2.sh

PL/SQL procedure successfully completed.


Contents of juletest2.sh:
#!/bin/sh
sqlplus -L -s system/SysOra1234@cloud_dev @cleanonegeo



I'm assuming it's because the ORACLE_HOME variable hasn't been set since I'm running it remotely, but what is a workaround so that I can run this script remotely?  What should I include in the juletest2.sh script to get it working?
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>I'm assuming it's because the ORACLE_HOME variable hasn't been set since I'm running it remotely

It is an environment issue but the PATH is more important than ORACLE_HOME.

>>what is a workaround so that I can run this script remotely?

In the script itself, properly set up the Oracle environment.  I cannot say exactly how because I don't know how your system is configured.


If things are set up normally you should be able to add the following to the top of the script:
ORAENV_ASK=NO
export ORACLE_SID=CLOUD_DEV
. oraenv


oraenv should set ORACLE_HOME based on the entry from /etc/oratab and add $ORCLE_HOME/bin to the front of PATH so sqlplus can be found.

If things aren't set up right, you can manually set PATH to locate sqlplus.
Another trick is to add a file to /etc/profile.d (or equivalent) to setup all your Oracle related environment vars.

This way CRON or external logins, any session always gets the same variables set.
Avatar of Christian Palacios

ASKER

Thanks for your replies!  
slightwv: I got this error when I put your code in the script:
[oracle@cne-ora05 ~]$ ssh oracle@10.41.22.69 /home/oracle/juletest2.sh
ORACLE_HOME = [/home/oracle] ?
ORACLE_BASE environment variable is not being set since this
information is not available for the current user ID oracle.
You can set ORACLE_BASE manually if it is required.
Resetting ORACLE_BASE to its previous value or ORACLE_HOME
The Oracle base has been set to /home/oracle
/home/oracle/juletest2.sh: line 5: sqlplus: command not found


David: I'm not familiar with the profile.d directory.  Would I have to create a oracle.sh file there so it matches the user ID?

Thanks,
- Christian
>>ORACLE_HOME = [/home/oracle] ?

Check the /etc/oratab file and make sure the database you wish to connect to is in there.  If not, get with your DBA.
or
Manually set PATH to include the path to sqlplus
or
Fully qualify the path to sqlplus in your script:  /some/path/to/it/sqlplus -L -s system/SysOra1234@cloud_dev @cleanonegeo

>>David: I'm not familiar with the profile.d directory.

It is a way to set up environments for ALL scripts.  If you want ALL bash scripts set up to run sqlplus, then you can set it there.  Might be overkill for some systems.  It all depends on your specific needs.

https://help.ubuntu.com/community/EnvironmentVariables#A.2Fetc.2Fprofile.d.2F.2A.sh
Thank you all.  I ended up adding the contents of the oracle profile file into the script so that it would initiate the required values and then the script began to work.  

- Christian
Don't forget to close this out by accepting the post or posts that helped you find a solution.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.