neelima reddy
neelima reddy

Reputation: 11

How to use Parallel and sequential execution of bash scripts in unix

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?

Upvotes: 1

Views: 1033

Answers (2)

Wargamer-Senpai
Wargamer-Senpai

Reputation: 21

    #!/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.

Upvotes: 2

yassir ait el aizzi
yassir ait el aizzi

Reputation: 62

you can try this :

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

Upvotes: -1

Related Questions