I have the following script:
command file1 &
command file2 &
command file3 &
command file4 &
command file5 &
command file6 &
# SOME OTHER COMMAND
command file7 &
command file8 &
command file9 &
command file10 &
command file11 &
command file12 &
At the place of "SOME OTHER COMMAND", I would like a command to halt the execution of the next commands until all of the processes prior to it are done so that I'm effectively running commands in "batches". How can I do that?
You should be able to use the bash shell's built in
wait
command. Fromman bash
(emphasis mine):So
If you want to get a bit fancy, a bash script
xargs
is a great option when you're running the same command with different arguments.The
-P 32
tells xargs to run at most 32 processes at a time. The-n 1
means pass at most one argument to the command (without it, its likely to send all the files to a single command, and thus get no parallel execution).So, in this case, set
-P
to your batch size. The other option (and benefit), is that you can set-P
to the amount of parallelism you want to actually use. While the&
approach will launch a process for each command, xargs will limit the number of processes to what you set. This means you can target parallelism to the number of cores you have, or use a smaller number to prevent a large batch process from bringing your machine to a crawl.Oh, and of course, replace
command
with your actual command.