4

i do have 3 scripts (A,B,C) that i would like one after the other only if the first one finished. At the moment if A.sh finishes it goes to B.sh, but if B.sh fails, then it will not execute C.sh any ideas on how to fix that, please? i am running them through cron like:

30 6,14 * * 1-5/home/user/scripts/A.sh ; /home/user/scripts/B.sh ; /home/user/scripts/C.sh ; 

Any ideas please?

2 Answers 2

7

A more elegant way to create that cron job would be:

30 6,14 * * 1-5 /home/user/scripts/A.sh && /home/user/scripts/B.sh && /home/user/scripts/C.sh

This will cause the three jobs to run consecutively. You could do $HOME/scripts/A.sh if you have your variables set accordingly.

For basic job control, e.g. ensuring that script-2 only runs if script-1 is successful, you could add a line for exit=0 to write a temp file such as /tmp/script-1_success.

Then at the beginning of script-2, have it check to see if /tmp/script-1_success exists. If not, have it exit.

3
  • thanks for the reply. i would like B.sh and C.sh to run no matter what if the B or C has failed.
    – john
    Commented Jun 8, 2017 at 7:01
  • 1
    @john In that case, creating a cron job as I stated in my answer should operate as you want.
    – jones0610
    Commented Jun 8, 2017 at 15:51
  • This is looks nice. However I would like to log what both script do in one log file. Is it possible?
    – marcin2x4
    Commented Jan 19, 2021 at 22:58
6

You can make a script which calls other scripts in an organized way, like following, This is your cron file,

30 6,14 * * 1-5/home/user/scripts/A-B-C.sh

Then the script A-B-C.sh is defined as,

#!/bin/bash
# Run script A,B,C

# execute script A
./home/user/scripts/A.sh

#execute script B
./home/user/scripts/B.sh

enter code here
#execute script C
./home/user/scripts/C.sh

If you want to \ do not want to run script B.sh and C.sh in cases where script A.sh has some error, then you can include error tracking using variables, whose value you can keep as 0 or 1. Then in case the previous script does not finish as expected you can take necessary decision about what to do with the next script. An example is the following,

#!/bin/bash
# Run script A,B,C

# Exit code and default run code initialization
exit_A=0
exit_B=0

def_B=1
def_C=1
#----------------------------------------------


# execute script A
. ./home/user/scripts/A.sh $exit_A #make sure to change the value of exit_A to 1 if A finishes as expected.

if [$exit_A==1] && [def_B==1] #only if A finishes without error and B is ok
then
#execute script B
. ./home/user/scripts/B.sh $exit_B
else
<do something like log>
fi

if [$exit_B==1] && [def_C==1] #only if B finishes without error and C is ok
then
#execute script C
. ./home/user/scripts/C.sh $exit_C
else
<do something like log>
fi

now="$(date +'%d/%m/%Y-%T')"
#make a log in a file (print can be used for better formatting)
echo -e $now,$exit_A,$exit_B,$exit_C >>  /home/user/log/script_log.txt 

In the above script, variables are being passed across scripts. Use with caution and test before using the code.

3
  • I assumed, perhaps incorrectly, that the OP wanted to use three autonomous scripts. Otherwise he could just write one long script. Also, there seemed to be a desire to only run successive scripts if the previous script exited without failures.
    – jones0610
    Commented Jun 8, 2017 at 6:38
  • Thanks for the remark. I also thought so and I was in the process of writing. :-)
    – ankit7540
    Commented Jun 8, 2017 at 7:25
  • how do you change the value to 1, if I finishes successfully?
    – Snow
    Commented Jan 8, 2020 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .