Home > Software engineering >  How to use Parallel and sequential execution of bash scripts in unix
How to use Parallel and sequential execution of bash scripts in unix

Time:03-08

I have 3 shell scripts a.sh, b.sh and c.sh. the scripts a.sh and b.sh are to be run parallely and script c.sh should only run if a.sh and b.sh are run successfully ( with exit code 0).

Below is my code. The parallel execution is working fine but sequential execution of c.sh is out of order. It is executing after completion of a.sh and b.sh even if both the scripts are not returning exit codes 0. Below is the code used.

#!/bin/sh

A.sh &

B.sh &

wait &&

C.sh

How this can be changed to meet my requirement?

CodePudding user response:

    #!/bin/bash
    ./a.sh & a=$!
    ./b.sh & b=$!
    
    if wait "$a" && wait "$b"; then
      ./c.sh
    fi

Hey i did some testing, in my a.sh i had an exit 255, and in my b.sh i had an exit 0, only if both had an exitcode of 0 it executed c.sh.

CodePudding user response:

you can try this :

.....
wait &&
    if [ -z "the scripts a.sh or any commande u need  " ] 
    then
       #do something
       C.sh
    fi
  • Related