Skip to content

Instantly share code, notes, and snippets.

@ErwanAliasr1
Last active March 23, 2016 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErwanAliasr1/3e332ff080c08c3e78f1 to your computer and use it in GitHub Desktop.
Save ErwanAliasr1/3e332ff080c08c3e78f1 to your computer and use it in GitHub Desktop.
#!/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