# I want to process a bunch of files in parallel, and when one finishes, I want
# to start the next. And I want to make sure there are exactly 5 jobs running
# at a time.

# For most of these examples, we will assume the task you want to run is a
# command, which takes a filename as an argument.
# Thus, you'll be feeding a stream of filenames into something that dispatches
# the job-commands.

# Many xargs allow running tasks in parallel, including FreeBSD, OpenBSD and
# GNU (but not POSIX). If your xargs has -0 and -P options, it's a great choice
# for this task.

find . -print0 | xargs -0 -n 1 -P 5 command

# One may also choose to use GNU Parallel (if available) instead of xargs, as
# GNU Parallel makes sure the output from different jobs do not mix.

find . -print0 | parallel -0 command | use_output_if_needed

# In a script where the loop is very big you can use sem from GNU Parallel.
# Here 10 jobs are run in parallel:

for i in ./*.log; do
  printf '%s\n' "${i}"
  # [...do other needed stuff...]
  sem -j10 gzip "${i}" ";" printf 'done\n'
done
sem --wait
