Search code examples
bashshellgnu-parallel

Running bash jobs in parallel with predefined order prioritization


I want to run 3 jobs (A, B, C) on 2 cores of a machine with >2 cores. I know that:

runtime(A)>runtime(C)

runtime(B)>runtime(C)

It is unknown in advance if runtime(A)>runtime(B) or runtime(A)<runtime(B).

What I want to do is:

  • launch A on core 1
  • launch B on core 2
  • after either one is finished, launch C on the one free core

How can this be achieved (in bash, if possible)?


Solution

  • Just tell GNU Parallel it can use 2 cores:

    parallel -j 2 ::: jobA jobB jobC
    

    Note that the jobs will run in the order you specified, but the output may come in a different order. If that is an issue, add -k parameter to keep output in order.