Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 2
    According to wait man pages, wait with multiple PID's only returns the return value of the last process waited for. So you do need an extra loop and wait for each PID separately, as suggested in the accepted answer (in comments). Commented Jul 6, 2015 at 19:17
  • 1
    Because it doesn't seem to be stated anywhere else on this page, I'll add that the loop would be for pid in $pids; do wait $pid; done Commented Jun 7, 2016 at 13:36
  • 1
    @bisounours_tronconneuse yes, you do. See help wait - with multiple IDs wait returns the exit code of the last one only, as @vlad-frolov said above. Commented Sep 28, 2016 at 9:28
  • 5
    I had an obvious concern with this solution: what if a given process exits before the corresponding wait is called? It turns out that this isn't a problem: if you wait on a process that's already exited, wait will immediately exit with the status of the already-exited process. (Thank you, bash authors!) Commented Mar 23, 2018 at 15:32
  • 2
    This was exactly what I needed, handles failures in either sub-process perfectly and ensures that the main process finishes (either early if either sub-process failed, or going on to the ...code continued here... if all sub-processes succeed) only once all sub-processes are completed. Commented Dec 4, 2019 at 16:35