Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andy Chu
committed
Mar 24, 2017
1 parent
e40c9e4
commit 2632ae5
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/bin/bash | ||
# | ||
# Usage: | ||
# ./background-status.sh <function name> | ||
|
||
set -o nounset | ||
set -o pipefail | ||
#set -o errexit # wait waill fail with this | ||
|
||
do-some-work() { | ||
sleep 0.2 | ||
exit $1 | ||
} | ||
|
||
wait-pids() { | ||
{ sleep 0.1; exit 5; } & | ||
pid1=$! | ||
|
||
do-some-work 6 & | ||
pid2=$! | ||
|
||
{ sleep 0.3; exit 7; } & | ||
pid3=$! | ||
|
||
do-some-work 8 & | ||
pid4=$! | ||
|
||
echo "Waiting for PIDs $pid1 $pid2 $pid3" | ||
|
||
wait $pid1; echo $? | ||
wait $pid2; echo $? | ||
wait $pid3; echo $? | ||
wait $pid4; echo $? | ||
|
||
echo 'Done' | ||
} | ||
|
||
wait-next() { | ||
{ sleep 0.1; exit 8; } & | ||
pid1=$! | ||
|
||
{ sleep 0.2; exit 9; } & | ||
pid2=$! | ||
|
||
{ sleep 0.3; exit 10; } & | ||
pid3=$! | ||
|
||
wait -n; echo $? | ||
wait -n; echo $? | ||
wait -n; echo $? | ||
|
||
echo 'Done' | ||
} | ||
|
||
both() { | ||
wait-pids | ||
wait-next | ||
} | ||
|
||
"$@" |