Start your script in a subshell and let each subprocess handle the errors.
(sh test1.sh || echo "test1" >> failure.txt)&
(sh test2.sh || echo "test2" >> failure.txt)&
Or perhaps without a subprocess:
sh test1.sh || echo "test1" >> failure.txt&
sh test2.sh || echo "test2" >> failure.txt&
Demo:
touch job1 job2 job4 job5 job7
rm result # when you repeat the demo
for ((j=1; j<8;j++)); do
(sleep 2; rm job$j 2>/dev/null || echo "job$j failed" >> result)&
done
wait
cat result
The result will show the files that you did not create.
job3 failed
job6 failed
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…