Want to take your long running commands and run them at the same time? You'll also want to:
- see the progress
- tail the output
- be notified if they error
We'll need code to hold our log files, process ids and wait for them to finish
# log files & process ids
LOG_FILEPATHS_ARRAY=()
PIDS_ARRAY=()
# process pids & echo errors if needed
process_pids () {
for pid in "${PIDS_ARRAY[@]}"
do
werr=0
wait $pid || werr=$?
if [ "$werr" != 0 ] && [ "$werr" != 127 ] ; then
echo ". failed: ${LOG_FILEPATHS_ARRAY[$pid]}"
else
echo "."
fi
done
}
With this we can kick off long running processes like so
# each command gets it's own logfile
log_filepath="/tmp/some_logfile_name.output"
# set command
CMD="<do_something_here>"
# log header
echo "" >> $log_filepath
echo $CMD >> $log_filepath
echo "" >> $log_filepath
# go!
$CMD >> $log_filepath 2>&1 &
# save pid & logfile
pid=$!
PIDS_ARRAY+=($pid)
LOG_FILEPATHS_ARRAY[$pid]=$log_filepath
Do the above for each command you want to run. Then wait for them to finish with the following
if [ ! -z "${PIDS_ARRAY:-}" ]; then
echo ""
echo " waiting for builds to finish..."
echo " tail -f /tmp/make.core.*.output"
echo ""
process_pids
else
echo " nothing to do.. did you 'make get' yet?"
fi
#
# DONE
#
printf " DONE"
echo ""
And Voila, which is French for...... and then it just works.