1

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?

2
  • What is the return of the last ID? Is it possible that A.sh or B.sh returned a non-zero value. Have you tried removing the "&&". Then C.sh will always be run Commented Mar 8, 2022 at 5:29
  • According to the POSIX standard, "If the wait utility is invoked with no operands, it shall wait until all process IDs known to the invoking shell have terminated and exit with a zero exit status" (my emphasis). Therefore, you need to wait for each separately to get the background processes' exit statuses. Commented Mar 8, 2022 at 7:53

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

You could simplify to just if wait "$a" && wait "$b"; then...
@tripleee yes looks cleaner 👍
-1

you can try this :

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

6 Comments

Should I use wait && in the beginning??
#!/bin/sh A.sh & B.sh & wait && if [ -z "the scripts a.sh or any commande u need " ] then #do something C.sh fi
just replace : C.sh with : if [ -z "the scripts a.sh or any commande u need " ] then #do something C.sh fi
If one of the scripts a or b takes longer will c still be run?
@yassir ait ek aizzi thank you...but, why is the if loop being used there, the purpose?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.