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
– pLumo Sep 12 '19 at 07:08for i in 1 2 3 4; do sleep $i & done; wait
. Spoiler: Yes it is sufficient. – pLumo Sep 12 '19 at 07:10parallel do_something ::: 1 2 3 4;
– pLumo Sep 12 '19 at 07:15(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). – Sep 12 '19 at 07:25