Rio mario
Rio mario

Reputation: 283

Running shell script only if parallel backgrounded scripts complete successfully

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?

Upvotes: 3

Views: 73

Answers (1)

anubhava
anubhava

Reputation: 785098

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

Upvotes: 3

Related Questions