Last active
March 23, 2016 18:15
-
-
Save ErwanAliasr1/3e332ff080c08c3e78f1 to your computer and use it in GitHub Desktop.
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 | |
function run_in_background() { | |
# The first argument is the name of the PID variable | |
# We execute everything passed in argument | |
# And save the running pid in the variable name | |
local pid_variable=$1 | |
shift; | |
"$@" & eval "$pid_variable+=\" $!\"" | |
} | |
function wait_background() { | |
return_code=0 | |
# We extract the PIDS from the variable name | |
for pid in ${!1}; do | |
if ! wait $pid; then | |
return_code=1 | |
fi | |
done | |
eval "$1=''" | |
return $return_code | |
} | |
pids1="" | |
run_in_background pids1 bash -c 'sleep 5; exit 0' | |
run_in_background pids1 bash -c 'sleep 1; exit 1' | |
wait_background pids1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment