Running bash jobs in parallel with predefined order prioritization

Viewed 93

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)?

2 Answers

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.

It's easy in Bash 4.3 (released in 2014) or later:

A &
B &
wait -n # wait for any child to finish
C &
wait # for everyone to finish
Related