Created
November 21, 2008 15:13
-
-
Save jzawodn/27452 to your computer and use it in GitHub Desktop.
how to wait on multiple background processes and check exit status in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FAIL=0 | |
echo "starting" | |
./sleeper 2 0 & | |
./sleeper 2 1 & | |
./sleeper 3 0 & | |
./sleeper 2 0 & | |
for job in `jobs -p` | |
do | |
echo $job | |
wait $job || let "FAIL+=1" | |
done | |
echo $FAIL | |
if [ "$FAIL" == "0" ]; | |
then | |
echo "YAY!" | |
else | |
echo "FAIL! ($FAIL)" | |
fi |
but how could you know the first subprocess in loop is the one finished first, if the later ones in loop finished early, what is the return code
that's a nice workaround.. but, using just the
wait
command (without passing the process ID) would have worked too? So that, it waits for all the background process to get complete../sleeper 2 0 & ./sleeper 2 1 & ./sleeper 3 0 & ./sleeper 2 0 & wait
Please let me know your thoughts.
(I got here from your blog post - http://jeremy.zawodny.com/blog/archives/010717.html)
So nice, thank you! It works in my script.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can also use trap, something like this: