The short version: My bash script has a function.
This function then launches several instances (a maximum of 10) of another function in the background (with &
).
I keep a count of how many are still active with jobs -p | wc -w
in a do loop. When I'm done with the loop, I break
.
I then use wait
to ensure that all those processes terminate before continuing.
However, when I check the count (with jobs -p
) I sometimes find this:
[10] 9311 Done my_background_function_name $param
How can I get wait
to only proceed when all the launched child-processes have completely terminated and the jobs list is empty?
Why are jobs sometimes shown with "Done" and sometimes not?
Clearly, my knowledge of how jobs
works is deficient. :)
Thanks.
jobs -p
why not just jump to thewait
since it'll wait for all the children? – Eric Renoufjobs -p
forms part of an on-screen progress display to let user know how many copies of that background function are still active. Afterwait
it runs again to ensure progress now reads "0". But sometimes it doesn't. I don't know why. Maybe I should parsejobs
output and count the number of jobs listed as 'Running' only? – teracowjobs -p
might be a better solution here. Now I'm not sure. :) – teracow