jonderry
jonderry

Reputation: 23633

How do I write a script to run multiple processes in the background and have additional commands run once each individual command completes?

I'm trying to do something like the following in a bash script:

for x in a b c; do echo foo $x; sleep 5 & y=$! ; (wait $y && echo bar $x) & done

Is this possible, without adding much complexity (the above example of course does not actually work)?

EDIT:

Of course in reality, echo foo $x; sleep 5 would be for example, copying some large file or compiling something big, and the second operation would be operations that depend on the first.

Upvotes: 2

Views: 156

Answers (1)

Andrew White
Andrew White

Reputation: 53496

Maybe I am missing something but what about using ()s and putting the & on the entire sub group command...

for i in 1 2 3; do (echo $i; sleep 5; echo end $i)& done

Upvotes: 6

Related Questions