Shell script to execute Oracle procedure and trigerring email on success and failure
Here is the shell script which need to trigger a stored procedure and when the record count is 0 then it should capture in the log that 0 duplicate records are found and if it's more than 0 then log with no of records. Also if there is any sqlerror then it should write the same in the log file and it should send the communication in all scenarios. We have funct file which has declared with all directories and we are referring here for the log file path directory. But when i tried to execute it getting this error message not found [No such file or directory] - ./local/dir1/funct.sh. Also can you help me out in the code logic as well?
PS - I have all the privileges to this file
Code:
#!/bin/ksh
./local/dir1/funct.sh
sqlplus -s "${ORAUSER}"/"${ORAPASSWD}"@"${ORASRVC}" <<EOF > "$LOG_TEXT"
set head off
set serveroutput on
SELECT TO_CHAR(SYSDATE,'MMDDRRRRHH24MISS') FROM DUAL;
EOF
TIME_STAMP=$(cat "${LOG_TEXT}")
LOG_FILE_NAME='FileStatus'${TIME_STAMP}'.log'
LOG_FILE=${LOGFILEDIR}'/FileStatus/'${LOG_FILE_NAME}
write_log "Database Timestamp is $TIME_STAMP"
write_log "Executing FileStatus now..."
chmod 777 "${LOG_FILE}"
sqlplus -s "${ORAUSER}"/"${ORAPASSWD}"@"${ORASRVC}" <<EOF >> "${LOG_TEXT}"
set feedback off
set heading off
DECLARE
v_count NUMBER;
BEGIN
select count(*) INTO v_count from (select col1,col2,count(col3) from Tab1 group by col1,col2 having count(col3)>1);
whenever sqlerror exit -1
whenever oserror exit -1
execute FileStatus;
END;
EOF
if [[ ${v_count} -eq 0 ]]; then
write_log "FileStatus executed successfully."
write_log "No Duplicate records Found: $LOG_FILE"
mailx -s "No Duplicate records Found" email< "${success}"
elif [[ ${v_count} -gt 0 ]];
then
write_log "FileStatus executed successfully."
write_log "Number of duplicate records found::$v_count:$LOG_FILE"
mailx -s "Number of duplicate records found:$v_count" email < "${success}""
else
Write_log "FileStatus Failure."
mailx -s "FileStatus" email < "${failure}""
fi
cleanup
exit 0;
Firstly I would suggest that you move the credentials to within the here document like this:
Code:
:
:
sqlplus -sn <<EOF > "$LOG_TEXT"
connect "${ORAUSER}"/"${ORAPASSWD}"@"${ORASRVC}"
set head off
:
:
The way you have it exposes the credentials to anyone who manages to run ps whilst the sqlplus command is running each time.
You can even neaten this further by avoiding the output file:
Code:
:
:
TIME_STAMP="$(sqlplus -sn <<EOF # Start the sub-process with "$(
connect "${ORAUSER}/${ORAPASSWD}@${ORASVC}" # Just double quote the whole thing here rather than each little bit
set head off
set serveroutput on
SELECT TO_CHAR(SYSDATE,'MMDDRRRRHH24MISS') FROM DUAL;
EOF)" # End of the sub-process with the )"
LOG_FILE_NAME="FileStatus${TIME_STAMP}.log" # Just double quote the whole
LOG_FILE="${LOGFILEDIR}/FileStatus/${LOG_FILE_NAME}" # ... and again
## Or perhaps go straight to LOG_FILE="${LOGFILEDIR}/FileStatus/FileStatus${TIME_STAMP}.log"
write_log "Database Timestamp is ${TIME_STAMP}" # Braces added to be consistent
write_log "Executing FileStatus now..."
chmod 777 "${LOG_FILE}"
:
:
As to your actual question over the 'not found' response (for which I've wrapped it in ICODE tags in your post) this is probably because the file is relative to your current working directory. If you doa pwd then think about where that file is relative to where you are thne it might become clearer. Can you set an absolute path (i.e. starts with a leading slash for the root directory) and try again? Does this get you anywhere further?
I hope tha these suggestions help,
Robin
Last edited by rbatte1; 12-19-2019 at 08:24 AM..
Reason: Properly aligning comments
I tried with the same code by giving the exact path for funct.sh. Now that error not found [No such file or directory got rectified but it's not connecting with Oracle and not executing the proc. Moreover am getting email message that No Duplicate records Found: though the result of this query fetches 800 records.
Code:
select count(*) INTO v_count from (select col1,col2,count(col3) from Tab1 group by col1,col2 having count(col3)>1);
Also I tried with other version of code with your suggested changes and got the error message like this
Code:
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
<proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus'.log: cannot create [No such file or directory]
Also , i'd like to have an accurate mechanism to store the return value of procedure to the shell script. Here my requirement is to execute a stored procedure from this shell script once done i need to trigger the emails based on their success with the record counts and if not return with sql error if any.Could you please help me on this?
Please can you post the code you ran to generate the error. It might be that the comments get in the way, so perhaps strip them out and try again too.
An alternate might be to return a complex string listing the return code and data values, then afterwards your script can separate out which means which. I do this for commands driven remotely by ssh connections. It's not pretty, but it works quite reliably.
Thanks for your reply Robin. Here is the modified code. Also could you assist me in how to store the return value of procedure to the shell script.Here the variable V_COUNT value is not returning to the shell script. Could you please give an example on this?
Code:
#!/bin/ksh
./local/dir1/funct.sh
TIME_STAMP="$(sqlplus -sn <<EOF
connect "${ORAUSER}/${ORAPASSWD}@${ORASVC}"
set head off
set serveroutput on
SELECT TO_CHAR(SYSDATE,'MMDDRRRRHH24MISS') FROM DUAL;
EOF)"
LOG_FILE_NAME="FileStatus${TIME_STAMP}.log"
LOG_FILE="${LOGFILEDIR}/FileStatus/${LOG_FILE_NAME}"
write_log "Database Timestamp is $TIME_STAMP"
write_log "Executing FileStatus now..."
chmod 777 "${LOG_FILE}"
ssqlplus -sn <<EOF > "$LOG_TEXT"
connect "${ORAUSER}"/"${ORAPASSWD}"@"${ORASRVC}"
set head off
DECLARE
v_count NUMBER;
BEGIN
select count(*) INTO v_count from (select col1,col2,count(col3) from Tab1 group by col1,col2 having count(col3)>1);
whenever sqlerror exit -1
whenever oserror exit -1
execute FileStatus;
END;
EOF
if [[ ${v_count} -eq 0 ]]; then
write_log "FileStatus executed successfully."
write_log "No Duplicate records Found: $LOG_FILE"
mailx -s "No Duplicate records Found" email< "${success}"
elif [[ ${v_count} -gt 0 ]];
then
write_log "FileStatus executed successfully."
write_log "Number of duplicate records found::$v_count:$LOG_FILE"
mailx -s "Number of duplicate records found:$v_count" email < "${success}""
else
Write_log "FileStatus Failure."
mailx -s "FileStatus" email < "${failure}""
fi
cleanup
exit 0;
Your first active line calls another script. Is that meant to load functions in for you? I'd suggest preceding it with the word source else any variables set and the functions defined will be lost when the script exits.
Line 18 exectuing chmod 777 "${LOG_FILE}" looks a little dubious. Would you really want anybody with access to the OS to be able to read your log (maybe), edit your log (not very likely) and potentially execute your logfile (almost certainly not)?
Does line 20 have a spelling mistake with ssqlplus?
Is line 36 calling a stored procedure? What's inside that? Does it end with a zero return code?
How do you know that there has been a failure, or is that just zero rows counted?
Outside of the embedded SQL, you refer to ${v_count} which only seems to be set within the embedded SQL, i.e. will only exist when the SQL process is running. It may be as simple as exiting from the SQL using that.
Have you considered finishing the SQL part with exit &v_count perhaps? It might be just as simple as that and you shell script will get the $? as the value of &v_count unless you abort early and return the value -1, of course.
Hi ,
I am trying to automate a gather stats in shell script
#!/usr/bin/ksh
export ORACLE_HOME=/orcl/app/oracle/product/11.2.0.1/db_1
export PATH="$PATH:$ORACLE_HOME/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$ORACLE_HOME/lib32"
export TNS_ADMIN=/opt/netprobe/config... (1 Reply)
i have the following code inside a shell script .prog in oracle server when i call the program
DBMS_OUTPUT.PUT_LINE(x_return_status|| ln_rep_req_id);
will return 0 , it is very strange , i try to submit the concurrent request in oracle , and it can successfully executed, what am i missing ? i... (1 Reply)
Hi Gurus,
Want to execute a shell script from a oracle procedure and get the status of the same, any assistance in this regard will be appreciated.
proc_data.sh is script name which I want to execute from oracle procedure
It should work something like below
procedure test
begin... (1 Reply)
Hi everyone!
I'm new with Shell Scripting, and I have to do a shell script to call a procedure, which have 2 input parameters, the directory(from server) and the txt file (which have informations to update/insert in DB).
I have to create a shell script to execute that procedure for each txt... (5 Replies)
Here's a shell script snippet.....
cd $ORACLE_HOME/bin
Retval=`sqlplus -s <<eof
$TPDB_USER/april@$TPD_DBCONN
whenever SQLERROR exit 2 rollback
whenever OSERROR exit 3 rollback
set serveroutput on
set pages 999
var status_desc char(200)
var status_code... (1 Reply)
Hi ,
i have created an .sh file that has the following code:
#!/bin/ksh
sqlplus -s p1istuat/p1istuat@CWS_IST6 @Procedure_Execute.sql &
sqlplus -s p1istuat/p1istuat@CWS_IST6 << EOF
exit
EOF
The mentioned Procedure_Execute.sql file inside has the following code:
exec TEST;
... (5 Replies)
hai,
can anybody say how to call or to execute an oracle stored procedure in oracle from unix...
thanks in advance.... for ur reply....
by,
leo (2 Replies)