Bash – running scripts concurrently

Hyperthreading in Bash

Sometimes in bash you need to run two scripts concurrently with one script and wait for them to finish. We can do it like this:

#!/bin/bash

echo Starting A ...
./a.sh && true || tee fail_A &

echo Starting B ...
./b.sh && true || tee fail_B &

wait < <(jobs -p)

test -f fail_A && echo A failed. && exit
test -f fail_B && echo B failed. && exit

echo "End..." 

Where script A is:

#!/bin/bash 
for i in {1..10} 
    do
        echo "A: "$i 
        sleep 1 
    done

script B is:

#!/bin/bash
for i in {1..10} 
do echo "B: "$i sleep 2 done

The main script will wait until scripts A and B, which are executing concurrently, complete. If either script A or B fails, the main script will not continue.

5/5 - (1 vote)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top