在 Windows 批处理循环中,如何在继续之前等待生成的进程完成 [英] In Windows batch loop, how to wait for spawned processes to complete before continuing

查看:31
本文介绍了在 Windows 批处理循环中,如何在继续之前等待生成的进程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在批处理文件中的循环内触发多个命令,如下所示:

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~1AmazonAWSCLIaws s3 cp s3://bucket1/%%x 
s3://bucket2/%%x --recursive
)

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

Start/wait 是否具有等待所有命令完成后再移动到循环后的下一行的效果?

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

推荐答案

start/wait 不是全局的",它只是等待启动的进程完成(也许...取决于应用程序是如何编程的).您需要的(并行启动多个进程并等待最后一个进程完成)可以用不同的方法完成:给所有进程一个定义的标题并观察它们:

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

这篇关于在 Windows 批处理循环中,如何在继续之前等待生成的进程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆