Skip to content

Commit

Permalink
Add a demo of background status.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Mar 24, 2017
1 parent e40c9e4 commit 2632ae5
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/background-status.sh
@@ -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
}

"$@"

0 comments on commit 2632ae5

Please sign in to comment.