Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to write a loop ('for') that will do some action 'X' number of times. But instead of performing the actions sequentially, one after another, I'd like them to execute simultaneously (in parellel at the same time). How can this be achieved? Script or a one liner are fine.

example ONLY:

for i in 1 2 3 4 5; do dd if=/dev/zero of=/tmp/disk$i bs=10M count=10; done
share|improve this question
up vote 3 down vote accepted

This is mostly along the answer Dennis gave (+1 there), with your sample case.

for i in $(seq 1 5); do \
 dd if=/dev/zero of=/tmp/disk$i bs=10M count=10 & \
done; wait

the '\' and new lines are just for reading-pleasure, you can write all that in one line.

This gives a better understanding of the control sequence and syntax,

for i in $(seq 1 5); do \
 ( \
  echo "$i starting ..."; \
  dd if=/dev/zero of=/tmp/disk$i bs=10M count=10; \
  echo "$i done ..."; \
 ) & \
done; wait
share|improve this answer
Since the OP tagged the question with Bash, you don't have to use seq. You can either do it as shown in the question or one of these ways: for i in {1..5} (Bash >= 3.0) or for ((i=1; i<=5; i++)) (Bash >= 2.04). However, for compatibility with other shells, seq can be used, unless you're on a BSD system such as FreeBSD or OS X, then you need to use jot. – Dennis Williamson May 17 '10 at 13:42
@Dennis, that quite collects the range options :-). I used seq from habit mostly... the for loops are good; seq lets you format easily, which may be the reason my mind sticks to it. – nik May 17 '10 at 15:55
I want to make my scripts portable (POSIX) so they can be used on as many systems as possible. I'll go with seq. Thanks to all respondents. – Felipe Alvarez May 18 '10 at 8:12

Add an ampersand (&) at the end of the dd command to make each one run in the background and add ; wait after the done.

In some cases, xargs --max-args=MAX-ARGS --max-procs=MAX-PROCS can be used.

share|improve this answer

If you have GNU Parallel http:// installed you can do this:

seq 1 5 | parallel dd if=/dev/zero of=/tmp/disk{} bs=10M count=10

Watch the intro video for GNU Parallel to learn more:

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .