Bash natively supports a wait
command.
Run parallel processes
Using default wait
behavior
By default, wait
will wait for all child processes to complete before continuing an execution. For most cases, this is sufficient.
|
|
Using the &
character at the ending of a line sends the process to the background to complete. Without wait
, the the script would end.
The script will complete in 2 seconds.
Output variation 1:
|
|
Output variation 2:
|
|
Using wait
with process ids
wait
allows for a process id argument value. This may be useful for establishing chained wait
s.
In the example below, the “hi3” task depends on “hi”; however “hi2” may run in parallel with “hi”.
|
|
Output:
|
|
The process id of the last execution is returned using $!
.