I want to execute the commands in a loop at the same time, not one after another

5

1

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

Felipe Alvarez

Posted 2010-05-17T08:25:19.623

Reputation: 1 666

Answers

4

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

nik

Posted 2010-05-17T08:25:19.623

Reputation: 50 788

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. – Paused until further notice. – 2010-05-17T13:42:39.887

@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 – 2010-05-17T15:55:40.960

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 – 2010-05-18T08:12:48.947

2

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.

Paused until further notice.

Posted 2010-05-17T08:25:19.623

Reputation: 86 075

1

If you have GNU Parallel http:// www.gnu.org/software/parallel/ 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: http://www.youtube.com/watch?v=OpaiGYxkSuQ

Ole Tange

Posted 2010-05-17T08:25:19.623

Reputation: 3 034