2

Is there a way to create say 10 instances of a process (for example yes) with a single command?

$instantiate 10 yes
5

Would something like the following be OK? This assumes bash (for brace expansion) and GNU parallel.

parallel -N0 -j0 -u yes ::: {1..10}

The -j0 setting is there to make sure as many processes as parameters get started, and -u (ungroup) is there so that the output of each process is printed as soon as it is available (this matters in the case of yes, since its output is infinite). -N0 prevents arguments from being inserted into the command line.

  • 1
    The \; : {} can be avoided by using -N0. If you want to run as many in parallel as possible -j0 is a better choise: It will automatically adjust if it cannot spawn new processes. – Ole Tange May 5 '15 at 5:53
  • Good point, I integrated your comment into my answer. – dhag May 6 '15 at 18:26
2

@dhag certainly has a one-line answer, but the syntax makes my eyes hurt. :)

Since you asked for a single command, and since the shell considers for-do-done a single (compound) command, I feel justified with this much more readable version:

for i in {1..10}; do yes &; done

Note that some shells automatically nice(2) background jobs, so if that's an issue then this isn't the best solution.

  • 1
    GNU parallel does have a strange syntax, but once you go through its tutorial for one hour, you'll thank yourself for the rest of your life for the powers it will arm you with :) – shivams May 5 '15 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.