In Windows batch loop, how to wait for spawned processes to complete before continuing

允我心安 提交于 2019-11-28 12:31:38

问题


I want to fire off multiple commands within a loop in a batch file like this:

for /l %%x in (20170101,1,20170105) do (
start /wait C:\Progra~1\Amazon\AWSCLI\aws s3 cp s3://bucket1/%%x 
s3://bucket2/%%x --recursive
)

#do something else here only when ALL the above commands complete

Will the Start /wait have the effect of waiting until all commands complete before moving to next line after the loop?


回答1:


start /wait isn't "global", it just waits for the started process to finish (maybe... depends on how the application is programmed). What you need (starting several processes in parallel and wait until the last is finished) can be done with a different method: give all your processes a defined title and watch them:

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,10) do (
   start "OneOfMyProcesses" timeout !random:~-1!
)
echo waiting
:loop
timeout 1 >nul
tasklist /v |find "OneOfMyProcesses" >nul && goto :loop
echo all of them are finished

Note: as mentioned above, this may or may not work with your application



来源:https://stackoverflow.com/questions/47657254/in-windows-batch-loop-how-to-wait-for-spawned-processes-to-complete-before-cont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!