Search code examples
shellparallel-processingwait

How to wait for a parallel command to finish before executing the next one ( Shell script)


I have 3 python commands that are executed in parallel:

command_1A & command_2A & command_3A &

What I need is to wait for each one separately and when either of them finishes, a different command should be executed. For example if command_1A finishes command_1B should be executed, if command_2A finishes command_2B should be executed and so on.

I found a solution to save the processes IDs and wait for all of them to finish, but that is not what I need. There might be an easy solution, but I am new to shell scripting and I could not find the answer anywhere.


Solution

  • Surely this is all you need:

    { command1A; command1B; } &
    { command2A; command2B; } &
    { command3A; command3B; } &
    

    That says to run:

    • command1A followed by command1B, in parallel with,
    • command2A followed by command2B, in parallel with,
    • command3A followed by command3B

    Don't try and omit any spaces or semi-colons to shorten the above.