2

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

$instantiate 10 yes

2 Answers 2

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.

2
  • 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
    Commented May 5, 2015 at 5:53
  • Good point, I integrated your comment into my answer.
    – dhag
    Commented May 6, 2015 at 18:26
1

@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.

3
  • 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
    Commented May 5, 2015 at 5:05
  • -bash: syntax error near unexpected token `;'
    – Cherona
    Commented Apr 19, 2022 at 22:50
  • Since you provided no other information (shell version, OS version, etc), my only response is, "You've got a typo."
    – Azhrei
    Commented Apr 20, 2022 at 23:53

You must log in to answer this question.

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