Skip to content

Instantly share code, notes, and snippets.

@babarot
Last active July 27, 2024 08:14
Show Gist options
  • Save babarot/37cb04224a2c032313c972e7ea1962fe to your computer and use it in GitHub Desktop.
Save babarot/37cb04224a2c032313c972e7ea1962fe to your computer and use it in GitHub Desktop.
Wait for a fix number of process in bash
for i in {1..10}
do
{
c=$((RANDOM % 6 + 1))
echo "start $c"
sleep $c
echo "end $c"
} &
if (( (i % 5) == 0 )); then
wait
fi
done
wait
cnt=1 # Use counter
for i in {1..10}
do
{
c=$((RANDOM % 6 + 1))
echo "start $c"
sleep $c
echo "end $c"
} &
if (( (cnt++ % 5) == 0 )); then
wait
fi
done
wait
@babarot
Copy link
Author

babarot commented Jul 27, 2024

$ ./parallel_wait.sh
start 3
start 6
start 6
start 5
start 2
end 2
end 3
end 5
end 6
end 6
start 1
start 1
start 4
start 1
start 5
end 1
end 1
end 1
end 4
end 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment