[svlug] Running a command n times simutaneously

Seth David Schoen schoen at loyalty.org
Tue Apr 25 11:40:00 PDT 2006

Andrew Stitt writes:

> On Tue, Apr 25, 2006 at 09:23:22AM -0700, Rufoo wrote:
> > For a performance measurement task I need to run a
> > command n>1 times simultaneously. Is there any tool
> > that can help me do this? 
> > 
> You can use the '&' shell operator to run a command in the background
> and use it in a loop to run the command in the background n times.
> in bash it looks like this:
> for ((i=0;i<n;i++)); do
>   cmd &
> done

New bash syntax always amazes me.  An older and much more portable
approach would be

for i in $(seq 10); do
  cmd &

If you don't mind getting a number as an argument to the command (or
if you want that behavior), you could do

seq 10 | xargs -P 0 -n 1 cmd

If you want to use xargs without putting anything on the command line,

seq 10 | xargs -P 0 -n 1 -i cmd

Seth David Schoen <schoen at loyalty.org> | This is a new focus for the security
     http://www.loyalty.org/~schoen/   | community. The actual user of the PC
     http://vitanuova.loyalty.org/     | [...] is the enemy.
                                       |          -- David Aucsmith, IDF 1999

More information about the svlug mailing list