Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running shell script only if parallel backgrounded scripts complete successfully

Tags:

linux

bash

unix

I have 4 bash scripts:

script1.sh
script2.sh
script3.sh
wrapper.sh

I want script1.sh and script2.sh to run in background. script1.sh and script2.sh should run at the same time (script2.sh should run even if script1.sh fails or vice versa). I want script3.sh to run only if script1.sh and script2.sh completes successfully.

wrapper.sh script should run all 3 scripts.

How can I achieve it?

like image 887
Rio mario Avatar asked Dec 03 '25 11:12

Rio mario


1 Answers

You can use warpper.sh like this:

#!/bin/bash

# execute script1.sh in background and save pid in $pid1
./script1.sh &
pid1=$!

# execute script2.sh in background and save pid in $pid2
./script2.sh &
pid2=$!

# do more work here...

# wait for both background jobs to finish and save their return status
wait $pid1 && wait $pid2 && ./script3.sh
like image 119
anubhava Avatar answered Dec 05 '25 00:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!