0

We have one unix job which always get failed due to following error

unable to execute os command

and just after retrying in second or third run it successfully executed.

So now I want retry logic up to 3 times for the below piece of code, please give example for below code.

If execution is successful in first run then it should come out of loop else it should continue retrying till 3 times. Then after it should come out of loop and process remaining code.

sqlplus -s / <<EOF
        set serveroutput on size 999999
        set feed off
        set term off
        spool ../logs/$PROGRAM.log3
        execute $PROGRAM;
EOF
 
James Z
  • 12,209
  • 10
  • 24
  • 44
vijay
  • 33
  • 7
  • [bash loops introduction](https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php) https://stackoverflow.com/questions/5274294/how-can-you-run-a-command-in-bash-over-until-success https://stackoverflow.com/questions/21982187/bash-loop-until-command-exit-status-equals-0 https://stackoverflow.com/questions/12967232/repeatedly-run-a-shell-command-until-it-fails https://stackoverflow.com/questions/30197818/run-command-until-successful-n-times-bash What research did you do? Where exactly are you stuck? – KamilCuk Aug 27 '20 at 11:44

1 Answers1

1

You could do something like this

iteration=0 
limit=3 
seconds_in_wait=10 

while [[ $iteration -le $limit ]];
do

sqlplus -s / <<EOF
        whenever sqlerror exit 99;
        set serveroutput on size 999999
        set feed off
        set term off
        spool ../logs/$PROGRAM.log3
        execute $PROGRAM;
EOF

if [[ $? -eq 99 ]];
then 
    if [[ $iteration -eq $limit ]];
    then 
        echo "Maximum times allowed - Error"
        exit 2;
    else
        iteration = $(expr $iteration + 1)
        sleep $seconds_in_wait 
    fi 
else
    iteration=$(expr $limit + 1)
fi 

done
  • You define a limit of iterations, in my example 3
  • If the process fails, you wait for a number of seconds and try again
  • If the process fails and you reach the limit, then you exit with error
  • If it does not fail, you exit the loop
Roberto Hernandez
  • 8,231
  • 3
  • 14
  • 43
  • Many thanks Roberto for your quick help i am beginner of Unix , i will put this code in my script and will let you know here – vijay Aug 27 '20 at 11:56
  • 1
    @vijay, thank you. I updated the answer to use the same syntax for the logic expressions ( -eq , -lt , -le ... ) – Roberto Hernandez Aug 27 '20 at 12:01