Skip to content

Instantly share code, notes, and snippets.

@mdelillo
Last active April 30, 2019 18:56
Show Gist options
  • Save mdelillo/8a8681494b8c07ca82771f9775c508c4 to your computer and use it in GitHub Desktop.
Save mdelillo/8a8681494b8c07ca82771f9775c508c4 to your computer and use it in GitHub Desktop.
Run batches of bash things in parallel
#!/usr/bin/env bash
set -euo pipefail
if [ "$#" -ne 1 ]; then
echo "Usage: $(basename "$0") <number-of-times-to-run-job>"
exit 1
fi
total_runs="$1"
int_regex='^[0-9]+$'
if ! [[ $total_runs =~ $int_regex ]] ; then
echo "Argument must be a positive integer"
exit 1
fi
job() {
n="$((RANDOM % 9 + 1))"
sleep "0.$n"
echo -e "$@"
}
runs_per_loop=10
number_of_loops=$((total_runs / runs_per_loop))
remainder=$((total_runs % runs_per_loop))
echo "number of loops: $number_of_loops"
echo "remainder: $remainder"
echo
if [[ "$total_runs" -gt "$runs_per_loop" ]]; then
for loop in $(seq 0 $((number_of_loops - 1))); do
for loop_run_num in $(seq 1 $runs_per_loop); do
num=$(((loop * runs_per_loop) + loop_run_num))
str="loop: $loop \tloop_run_num: $loop_run_num \trun_num: $num"
job "$str" &
done
wait
echo -e "\n[$(date)] Finished $(((loop + 1) * runs_per_loop)) runs\n"
done
fi
if [[ "$remainder" -ne 0 ]]; then
for loop_run_num in $(seq 1 $remainder); do
num=$(((number_of_loops * runs_per_loop) + loop_run_num))
str="final loop \tloop_run_num: $loop_run_num \trun_num: $num"
job "$str" &
done
wait
echo -e "\n[$(date)] Finished $total_runs runs"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment