I have a script with
for i in 1 2 3 4; do
do_something $i &
done
And when I call it, it terminates before all do_something
terminated. I found this question with many different answers.
Edit: help wait
tells me that
If ID is not given, waits for all currently active child processes, and the return status is zero.
Is it not sufficient to just add a single wait
at the end?
wait
is a shell builtin. Usehelp wait
instead ofman wait
for i in 1 2 3 4; do sleep $i & done; wait
. Spoiler: Yes it is sufficient.(do_something $i &)
(ie in a subshell) yourwait
will not wait for it, because the actualdo_something
process will be reparented to pid 1 (init).