[英]How to wait until everything started by a shell script ends?
我正在使用嵌套的shell腳本。
我試圖從中獲得解決方案,但未成功。
在我的OuterMostShellScript.sh中 ,我執行以下操作:
some commands
./runThisScriptX.sh
other commands
end of script.
runThisScriptX.sh包含一個使用&
運算符在后台運行某些進程的循環。
我希望由./runThisScriptX.sh
命令啟動的每個進程在控件移至之前完成,我說上述代碼中的other commands
行。
如何做到這一點?
編輯:我也這樣做:
some commands
./runThisScriptX.sh
wait
other commands
end of script.
但它沒有用。
兩件事情:
wait
您的腳本看起來不會像:
some commands
. ./runThisScriptX.sh # Note the leading . followed by space
wait # This would wait for the sourced script to finish
other commands
end of script
使用bash內置的wait
; 從手冊頁 -
等待每個指定的進程並返回其終止狀態。 每個n可以是進程ID或作業規范; 如果給出了作業說明,則將等待該作業管道中的所有進程。 如果未給出n,則等待所有當前活動的子進程,並且返回狀態為零。 如果n指定不存在的進程或作業,則返回狀態為127。否則,返回狀態為等待的最后一個進程或作業的退出狀態。
或者,不要后台執行任務。
在runThisScriptX.sh
內部,您應該等待並行子級完成后再退出:
child1 &
child2 &
child3 &
wait
然后在OuterMostShellScript.sh
,在后台運行runThisScriptX.sh
本身,然后等待它。
some commands
./runThisScriptX.sh &
wait
other commands
end of script.
wait
只能用於等待當前shell啟動的進程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.