I have the following bash script, which starts a program several times in parallel and passes a control variable to each execution. The program is utilizes several resources, so after it was started 10 times in parallel I want to wait till the last 10 started, are finished.
I am currently doing this very roughly, by just waiting after 10 iterations for the longest time possible that 10 parallel started programs are finished.
Is there a straight forward way to implement this behavior?
steps=$((500/20))
echo $steps
START=0
for((i=START;i < steps; i++))
do
for((j=START;j < steps;j++))
do
for((k=START;k < steps;k++))
do
n=$(($j*steps +$k))
idx=$(($i*$steps*$steps + $n))
if ! ((idx % 10)); then
echo "waiting for the last 10 programs"
sleep 10
else
./someprogram $idx &
fi
done
done
done