cron run a script after the other

17,082

Solution 1

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.

Solution 2

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.

Share:
17,082

Related videos on Youtube

john
Author by

john

Updated on September 18, 2022

Comments

  • john
    john over 1 year

    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?

  • jones0610
    jones0610 almost 7 years
    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.
  • john
    john almost 7 years
    thanks for the reply. i would like B.sh and C.sh to run no matter what if the B or C has failed.
  • ankit7540
    ankit7540 almost 7 years
    Thanks for the remark. I also thought so and I was in the process of writing. :-)
  • jones0610
    jones0610 almost 7 years
    @john In that case, creating a cron job as I stated in my answer should operate as you want.
  • Snow
    Snow over 4 years
    how do you change the value to 1, if I finishes successfully?
  • marcin2x4
    marcin2x4 over 3 years
    This is looks nice. However I would like to log what both script do in one log file. Is it possible?