This is a simple tool to parallelize execution of multiple tasks in Bash, since env_parallel
can't handle the environment size (or something of the sort - dies for mysterious reasons.) I'd love to hear your critique.
#
# Simple parallel executor. Takes a list of commands and executes each in its own child process. Assumes commands are distinctly named.
# Returns with 0 if all are successful. Otherwise, prints out their stdout/stderr outputs and returns with 1.
function simple_par () {
local TMP_FILE="${WRITABLE_DIR}/simple_par_$(uuidgen)"
touch $TMP_FILE
for prog in "$@"
do
local PROG_TMP="${TMP_FILE}_${prog}_$(uuidgen)"
(
( $prog 1>$PROG_TMP 2>&1 )
echo "$?" >> $TMP_FILE
) &
done
while true
do
RESULT_COUNT=$(wc -l ${TMP_FILE} | awk '{ print $1 }')
if [[ $RESULT_COUNT -eq $# ]]; then
if [[ $(cat $TMP_FILE) == *"1"* ]]; then
echo "Failed at least one command"
for prog in "$@"
do
echo "OUTPUT FOR: $prog"
cat ${TMP_FILE}_${prog}_*
rm ${TMP_FILE}_${prog}_*
done
rm $TMP_FILE
return 1
else
return 0
fi
fi
sleep 5
done
}