How to exit the bash script when one of its background processes has error?
<p>I have a bash script running two subscripts t1 and t2. 
Both t1 and t2 are background processes and t1 will raise an error. How do I catch this error and exit the whole script completely?</p>

<pre><code>#!/bin/bash
set -e

error1() {
    echo "exit whole script!!!"
    exit 1
}

# this script will rise an error
./t1.sh &amp;
pid1=$!


./t2.sh &amp;
pid2=$!


wait $pid2

if [ $pid2 -eq 0 ]; then
    trap 'error1' ERR
fi


wait $pid1

if [ $pid1 -eq 0 ]; then
    trap 'error1' ERR
fi
</code></pre>