Pages

Sunday, June 22, 2014

How to run multiple commands, at the same time ?

BASH - How to run multiple commands, at the same time ?

TL;DR

command1 > /tmp/output1 &
command2 > /tmp/output2 &
wait
comm1_result=$(</tmp/output1)
comm2_result=$(</tmp/output2)

The long way

When rapidly developing, you might start with:
comm1_result=`command1`
comm2_result=`command2`
This will obviously execute both commands one after the other,
so you might add:
comm2_result=`command2 &`
or
comm1_result=`command1` &
comm2_result=`command2` &
Which seemingly, should solve the issue. The problem here is that it serialize on stdout, so eventually they will run one after the other.

The complete solution, is as shown above to redirect output of both commands into two separate files than wait for the commands to complete and read the output into variables.