Created
July 9, 2014 15:44
-
-
Save VojtechVitek/90234efd6589074db42e to your computer and use it in GitHub Desktop.
Bash script - Run commands/jobs in parallel and attach to its output and wait for the return codes sequentially
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
# Run job in background, redirect its output to a non-blocking pipe, | |
# and leave the rc and stdout/stderr to be handled by attach_wait_jobs() | |
jobs_tmp_dirs="" | |
function run_job() { | |
local dir=$(mktemp -d -t gearXXXXXX) | |
jobs_tmp_dirs="$jobs_tmp_dirs $dir" | |
mkfifo $dir/pipe | |
mkfifo $dir/pipe_nonblock | |
"$@" &>$dir/pipe_nonblock & | |
echo $! >$dir/pid | |
cat $dir/pipe_nonblock | cat >$dir/pipe & | |
} | |
# Attach to all jobs being executed by run() one by one in the | |
# execution order, wait for them to finish and return on error | |
function attach_wait_jobs() { | |
for dir in $jobs_tmp_dirs; do | |
cat $dir/pipe | |
wait $(<$dir/pid) | |
local rc=$? | |
[ $rc -eq 0 ] || exit $rc | |
done | |
cleanup_jobs | |
} | |
# Terminate jobs and cleanup temporary files | |
trap 'cleanup_jobs' EXIT | |
function cleanup_jobs() { | |
local rc=$? | |
{ pkill -TERM -P $$; } &>/dev/null || : | |
for dir in $jobs_tmp_dirs; do | |
rm -rf $dir || : | |
done | |
jobs_tmp_dirs="" | |
return $rc | |
} | |
# USAGE: | |
# run_job <command-1> <arguments> | |
# run_job <command-2> <arguments> | |
# run_job <command-3> <arguments> | |
# attach_wait_jobs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment