heinistic
heinistic

Reputation: 941

Run two shell scripts in parallel then run another one after both finished

I have 3 shell scripts, I need to run the first two in parallel then run the third one after both (first two) scripts finished. How do I do this? All I know is running the first two scripts in background.

sh script1.sh &
sh script2.sh &
sh script3.sh &

I believe the third script will still continue with this one.

How would the third script 'wait' for them?

Upvotes: 3

Views: 272

Answers (2)

hek2mgl
hek2mgl

Reputation: 157947

Use the wait builtin. It should be:

sh script1.sh &
sh script2.sh &
wait
sh script3.sh

Upvotes: 3

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10064

GNU parallel is a tool to execute commands in parallel:

parallel -j2 ::: './script1.sh' './script1.sh' && ./script3.sh

Both script1 and script2 will be executed in parallel and after both are finished script3 will be executed.

Parallel's tutorial shows more advanced behavior customization, in case it is needed

Upvotes: 0

Related Questions