并行运行两个shell脚本,然后在两个完成后运行另一个

时间:2017-12-28 14:47:22

标签: linux shell

我有3个shell脚本,我需要并行运行前两个脚本然后在两个(前两个)脚本完成后运行第三个脚本脚本。我该怎么做呢?我所知道的是在后台运行前两个脚本。

sh script1.sh &
sh script2.sh &
sh script3.sh &

我相信第三个脚本仍将继续使用此脚本。

第三个脚本如何“等待”它们?

2 个答案:

答案 0 :(得分:3)

使用内置的wait。它应该是:

sh script1.sh &
sh script2.sh &
wait
sh script3.sh

答案 1 :(得分:0)

GNU parallel是一个并行执行命令的工具:

parallel -j2 ::: './script1.sh' './script1.sh' && ./script3.sh

script1 script2 将并行执行,两者完成后将执行 script3

Parallel's tutorial会在需要时显示更高级的行为自定义