Is there a way to create say 10 instances of a process (for example yes) with a single command?
$instantiate 10 yes
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.
\; : {}
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.
Commented
May 5, 2015 at 5:53
@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.