What is the use of TNS_ADMIN variable in Oracle? - oracle

Please tell me what is the use of TNS_ADMIN parameter in Oracle? I am working on Unix using oracle database.
Is this parameter is required to locate the sqlplus. I am executing a script in which a update query is executed on Oracle Database.
The script fails with 127 error code when executed with crontab.
The script contents I suspect (eval) failing are
----------
cmd='sqlplus ${ORALOGIN} < SQLS
----------
eval $cmd

TNS_ADMIN tells sqlplus where to find the tnsnames.ora file.
If you are running sqlplus from a crontab then the normal reason for having difficulty are:
Incorrect path
Not having the correctly set ORACLE_SID or other Oracle connection information
A startup/login script that is getting executed when you login to the system that is interfering with your cron execution
Some script that you run from the command line when you login that sets up your Oracle environment that is not getting executed in your crontab.
Check these things and other environment related items. It always takes me a number of passes to get crontab and Oracle to work happily together.

Related

Trying to execute sql script using sqlplus from powershell commandline

I'm trying to execute a sql script from powershell using the following command:
sqlplus username/password#tnsnamesalias 'path to my sql file.sql'
If I run the command without the script path, I can connect to the database and execute commands. If I include the script path (which includes spaces) then I just get the sqlplus help text and no changes are made to the database. My sql script is finished with END; and /
What am I doing wrong?
I believe you need to add the # sign before the path:
sqlplus username/password#tnsnamesalias #'path to my sql file.sql'
You have forgotten "#" symbol and the apostrophes are wrong here.
This works for me for executing a "test script.sql" file
sqlplus .... "#test script.sql"

sqlplus does not execute the query if it is called by a ssh external connection

I have a script lying into a Unix server which looks like this:
mainScript.sh
#some stuff here
emailScript.sh $PARAM_1 $PARAM_2
#some other stuff here
As you can see, mainScript.sh is calling another script called emailScript.sh.
The emailScript.sh is supposed to perform a query via sqlplus, then parse the results and return them via email if any.
The interesting part of the code in emailScript.sh is this:
DB_SERVER=$1
USERNAME=$2
PASSWORD=$3
EVENT_DATE=$4
LIST_AUTHORIZED_USERS=$5
ENVID=$6
INTERESTED_PARTY=$7
RAW_LIST=$(echo "select distinct M_OS_USER from MX_USER_CONNECTION_DBF where M_EVENT_DATE >= to_date('$EVENT_DATE','DD-MM-YYYY') and M_OS_USER is not null and M_OS_USER not in $LIST_AUTHORIZED_USERS;" | sqlplus -s $USERNAME/$PASSWORD#$DB_SERVER)
As you can see, all I do is just creating the variable RAW_LIST executing a query with sqlplus.
The problem is the following:
If I call the script mainScript.sh via command line (PuTTy / KiTTy), the sqlplus command works fine and returns something.
If I call the script mainScript.sh via an external job (a ssh connection opened on the server via a Jenkins job), the sqlplus returns nothing and takes 0 seconds, meaning it doesn't even try to execute itself.
In order to debug, I've printed all the variables, the query itself in order to check if something wasn't properly set: everything is correctly set.
It really seems that the command sqlplus is not recognized, or something like this.
Would you please have any idea on how I can debug this? Where should I look the issue?
You need to consider few things here. While you are running the script, from which directory location you are executing the script? And while you are executing the script from your external application from which directory location it is executing the script. Better use full path to the script like /path/to/the/script/script.sh or use cd /path/to/the/script/ command to go to the script directory first and execute the script. Also check execute permission for your application. You as an user might have permission to execute the script or sql command but your application does not have that permission. Check the user id for your application and add that into the proper group.

sqlplus command not found zabbix

I'm working and discovering the world of Zabbix. In particular I am trying to monitor an Oracle database with the Zabbix server through an external script. Given that other external scripts work, however, I created one with sqlplus, but on Zabbix I get "command not found". Can you tell me why?
The code is:
check.pl
#!/usr/bin/perl
use strict;
use warnings;
my $out=`echo "select * from v$version;" | sqlplus user/password#ip_database:port`;
print $out;
The code is very simple.
I created an item as always, passed as type "external check" and a key I entered my script. Can anyone solve my problem? Also if I was not clear, just ask for more information rather than "insult" on the forum: Thanks to everyone in advance
I RESOLVED IT WITH:
echo "/usr/lib/oracle/11.2/client64/lib" > /etc/ld.so.conf.d/oracle.conf
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle/11.2/client64/lib" >> /etc/profile
THANKS TO ALL!!!!
Apparently, your zabbix server does not have the necessary environment to find sqlplus. You could simply use the full path to sqlplus in your script (but that alone might not be enough) or create a wrapper script that sets all the necessary environment variables for your script.
From TFM:
The command will be executed as the user Zabbix server runs as, so any
access permissions or environment variables should be handled in a
wrapper script, if necessary, and permissions on the command should
allow that user to execute it.
You also have to configure the sqlplus libraries required to run sqlplus. The script which you use to start the zabbix server you can configure below oracle things in the startup script so that zabbix can find all necessary libraries to run.
export ORACLE_HOME={path to Oracle Client}
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}/lib
If still there are issues related to .so files then there must be some issues in your SQL client installation.

Exit from the oracle user session when running a script

I have a problem with a bash script on an Ubuntu system. I need to program a script which connects to oracle an spool some queries to text files.
Almost all of this tasks are acomplished but the script doesn't run as it should be.
To generate the connection to oracle I use the following lines inside my script:
su oracle
export ORACLE_SID=DB_SID
export ORACLE_HOME=[ORACLE_PATH]
export TWO_TASK=[HOSTNAME:PORT]
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -s usr/pass << EOF
In the subsequent lines I make all the spooling of the data and finally for exiting from sqlplus I use the following lines:
quit;
To diconnect from sqlplus
exit
To exit from the su oracle session
EOF
And after the EOF tag I put some other commands to be executed.
Problem is when I run my script:
user# sh MyScript.sh
Instead of doing all the tasks, the script only executes the lines to the point of the << EOF and returns to me the control of the terminal still logged as the oracle user.
oracle#user#
If I type 'exit' and press enter then the rest of the script is executed.
I need the script to execute from start to finish without this middle step but I don't know how.
Thanks in advance.
In the end it's not neccesary to use the [su oracle] to make the export of the environment variables so I simply deleted the line:
su oracle
And the process worked like a charm.
Thanks for all who helped.

Does SQLPlus exit after running a script?

We are using CA Workload Control Center (Autosys) to run a windows batch file which calls another batch file which calls SQLPlus with the following:
sqlplus username/password %1 %2 %3
Since this is all being done remotely I cannot see what is actually happening on the server.
I am wondering if the sqlplus file exists but has invalid syntax (like referring to non-existent table) will the sqlplus prompt exit and return back to the batch file?
The reason I ask is because I am looking at the log file from Autosys and it shows that SQLPlus call was made, it prints out ERROR AT LINE 2: Invalid Table, but then it does not show any other activity with the batch script after that where there are multiple echoes and file copies etc. It seems as though it is not exiting SQLPlus perhaps?
Is there a parm I need to pass to SQLPlus to tell it to exit SQLPlus and return back to the calling script after running a SQL script if it fails?
Edit: We are using "WHENEVER SQL ERROR" inside of our SQL files as well and the log file does show this:
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
But I am still expecting that it should continue with the rest of the Batch script but its not, above is the last that Autosys shows in the log
See SQLPlus instruction WHENEVER SQLERROR, it describes in Oracle docs:
http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm
Found the solution SO Post:
You can do sqlplus -l test/Test#mydatabase #script.sql; the -l flag means it will only try to connect once, and if it fails for any reason will exit instead of prompting. Look at the output of sqlplus -?, or see the documentation.

Resources