Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mohamed azeem.

Asked: November 29, 2019 - 3:56 am UTC

Last updated: February 17, 2020 - 2:23 am UTC

Version: 12c

Viewed 1000+ times

You Asked

I have written a shell script which will call two plsql procedures,
Firts I have connected to sqlplus then I will call a plsql inside a block for declaring the variables to pass a argument for the procedure.inside this block I have mentioned dbms output enable and dbms output.print to see the result of the procedure.
After that based on the error code output inside the plsql I will consider it as success or declin.
Then after this inside the shell script I will declare a another block to call a another plsql procedure like the same steps as like the first procedure.

The thing is even shell is not echoing the msg or the dbms output is not printing

The first procedure will take 4 hrs to complete( if I run it manually).the second one will take just 3 minutes.


I have scheduled this shell script as a cronjob in Linux.






If I change the order like 3 minutes procedure first and 4 hrs procedure as second
The script is echoing dbms output and success msg.
And for the second one nothing happens
No dbms output and even no script echoing happens.
Note:I have given dbms output size as 100000
and also If I execute the script manually it's working fine and echoing alll dbms and script msg into the cron log
Which I have mentioned in the crontab command

Help me on this.
===========================
program is,



#!/bin/bash

org="'0001'"
upd="'Y'"


ORACLE_HOME=/oracle/app/12.1.0.2/client_1/ ; export ORACLE_HOME
PATH=/usr/bin/gcc//:/usr/local/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr1/UBCEOD/.local/bin:/usr1/UBCEOD/bin:/oracle/app/12.1.0.2/client_1/bin/:/usr1/UBCEOD/bin:/oracle/app/12.1.0.2/client_1/bin/ ; export PATH
CONNECT=INTELLECTCARDS/INTELLECTCARDS@UBCCARDS; export CONNECT

#ORACLE_HOME=/oracle/app/product/12.1.0.2.0_32bitclint/; export ORACLE_HOME
#PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr1/SIR14234/bin:/oracle/app/product/12.1.0.2.0_32bitclint/bin ; export PATH
#CONNECT=INTELLECTCARDS/INTELLECTCARDS@SIR14235; export CONNECT


check_date=`sqlplus -s $CONNECT <<EOF
set serveroutput on feedback off
SELECT TODAY_DATE FROM DATE_TAB; 
EXIT;
EOF`

echo $check_date;

echo "=====================START==============================="
echo "todays date is :  $check_date "

echo "start time is :"

date

output_calc=`sqlplus -s $CONNECT <<EOF
 set serveroutput on feedback off
  DECLARE
     org_pass  varchar(15):=$org;
     upd_pass varchar(5):=$upd;
     txt_out  varchar(15):=NULL;
     txt_err varchar(256):=NULL;

  BEGIN
 dbms_output.enable();

 INTELLECTCARDS.OUTSTANDING_CALC_PROCEDURE(org_pass,upd_pass,txt_out,txt_err);

 dbms_output.put_line('txt output is'||txt_out||'txt msg is:'||txt_err);
  END;
/
exit;
EOF`

echo $output_calc;

if [ "$output_calc" -ne "00" ];then
echo "failed calc procedure and err code is:  $output_calc"
echo "the exec time for calc and end time is :"
date 
echo "====================CALC ABORT==========================="
exit 0;
else
echo "success calc procedure and err code is:  $output_calc"
echo "the exec time for calc and end time is :"
date 
fi



  output_recalc=`sqlplus -s $CONNECT <<EOF
 set serveroutput on feedback off
  DECLARE
     org1_pass  varchar(15):=$org;
     upd1_pass varchar(5):=$upd;
     txt1_out  varchar(15):=NULL;
     txt1_err varchar(256):=NULL;

  BEGIN
 dbms_output.enable();

 INTELLECTCARDS.OUTSTANDING_RECALC_CYCCODE(org1_pass,upd1_pass,txt1_out,txt1_err);

 dbms_output.put_line('txt output is'||txt1_out||'txt msg is:'||txt1_err);
  END;
/
exit;
EOF`

echo $output_recalc;


if [ "$output_recalc" -ne "00" ];then
echo "failed re calc procedure and err code is:  $output_recalc"
echo "the exec time for failed re calc :"
date 
echo "================RECALC ABORT============================="
exit 0;
else
echo "success  re calc procedure and err code is:  $output_recalc"
echo "the exec time for success re calc :"
date
fi


echo "=====================END================================="

and cron log is,

TODAY_DATE ---------- 20191205
=====================START===============================
todays date is :  
TODAY_DATE
----------
  20191205 
start time is :
Thu Dec  5 01:01:01 IST 2019

cron comand i have used is,

01 01 * * * sh /usr1/UBCEOD/prod/JOB_CHECK/OTB_CORRECT_real_1.sh >> /usr1/UBCEOD/prod/JOB_CHECK/chk_otb.log






and Connor said...

Sorry - I don't understand how this can ever work:

output_calc=`sqlplus -s $CONNECT <<EOF
 set serveroutput on feedback off
  DECLARE
     org_pass  varchar(15):=$org;
     upd_pass varchar(5):=$upd;
     txt_out  varchar(15):=NULL;
     txt_err varchar(256):=NULL;

  BEGIN
 dbms_output.enable();

 INTELLECTCARDS.OUTSTANDING_CALC_PROCEDURE(org_pass,upd_pass,txt_out,txt_err);

 dbms_output.put_line('txt output is'||txt_out||'txt msg is:'||txt_err);
  END;
/
exit;
EOF`

echo $output_calc;

if [ "$output_calc" -ne "00" ];then


Your dbms_output will return some text ("txt outout .....")

So how will that ever be "00" ?

I'm not a great fan of "var=`sqlplus command`" style of coding because its too restrictive. Its a little more work, but look at spooling and grepping eg

print "
connect user/pass
set serverout on
select ....
exec my_proc ...
etc etc" | sqlplus /nolog 1>/tmp/output.$$ 2>&1


so that you capture ALL of the output, and then you look for particular prefixes for variables. For example, your proc might do:

dbms_output.put_line('RESULT='||result);

and then in your script, you would have:

res=`grep ^RESULT= /tmp/output.$$ | awk -F= '{print $2}'`


or similar. That also opens up better error detection, eg

if [ "`grep ORA- /tmp/output.$$`" ] ; then
  mailx -s "AGGGGH" connor@support.com < /tmp/output.$$
  exit 1
fi

etc etc


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library