我有4个bash脚本:
script1.sh
script2.sh
script3.sh
wrapper.sh
我希望script1.sh和script2.sh在后台运行。 script1.sh和script2.sh应该同时运行(即使script1.sh失败,script2.sh也应该运行,反之亦然)。我希望script3.sh仅在script1.sh和script2.sh成功完成时运行。
wrapper.sh脚本应该运行所有3个脚本。
我怎样才能实现它?
答案 0 :(得分:3)
您可以像这样使用warpper.sh
:
#!/bin/bash
# execute script1.sh in background and save pid in $pid1
./script1.sh &
pid1=$!
# execute script2.sh in background and save pid in $pid2
./script2.sh &
pid2=$!
# do more work here...
# wait for both background jobs to finish and save their return status
wait $pid1 && wait $pid2 && ./script3.sh