我有两个脚本。第二是依赖于First。但是第一个脚本有后台进程所以第二个调用没有完成第一个脚本的所有后台进程。无论如何等待或停止第二个脚本直到第一个脚本的所有后台进程完成。我在下面添加示例代码:
#!/bin/sh <br>
# This is First Script!
echo script 1 start here # This is a comment, too!
for number in 1 2 3 4 5
do
echo $number & # Here five backgroud process triggers
done
echo Script one ends here
#!/bin/sh
# This is Second Script!
echo Script 2 starts </t># This is a comment, too!
for number in 1 2 3 4 5
do
echo $number & # here other five background process triggers
done
echo Script 2 ends here
答案 0 :(得分:2)
使用$!记录任何后台进程的pid并使用它来确定辅助脚本的执行点。
你原来的脚本列出了sh,wait是bash的一个功能。如果可能的话,使用bash。 sh
例如:
#!/bin/bash
echo "I'm Starting Now"
sleep 60 & #this process now executes in the bg
bg_pid=$!
wait $bg_pid #this will pause the script until pid is dead.
echo "You will not see this until sleep is complete"
要将其合并到您发布的脚本中,它将如下所示:
#!/bin/bash
# This is First Script!
echo 'script 1 start here' # This is a comment, too!
for number in 1 2 3 4 5
do
echo $number & # Here five background process triggers
declare bgpid[$number]=$! #we will pack all the pids into an array
done
wait ${bgpid[@]} #this exposes the array to wait
echo 'Script one ends here'
#Call next script