Batch file 等待通过调用批处理文件创建的进程完成
Batch file 等待通过调用批处理文件创建的进程完成,batch-file,command-line,command-prompt,windows-shell,Batch File,Command Line,Command Prompt,Windows Shell,MyFile1.bat调用MyFile2.bat两次: start MyFile2.bat argA, argB, argC start MyFile2.bat argX, argY, argZ 此时,如何等待对MyFile2.bat的调用产生的两个进程都完成 start /w cmd /c "start cmd /c MyFile2.bat argA, argB, argC & start cmd /c MyFile2.bat argA, argB, argCt" 根据我的测试,如
MyFile1.bat
调用MyFile2.bat
两次:
start MyFile2.bat argA, argB, argC
start MyFile2.bat argX, argY, argZ
此时,如何等待对MyFile2.bat
的调用产生的两个进程都完成
start /w cmd /c "start cmd /c MyFile2.bat argA, argB, argC & start cmd /c MyFile2.bat argA, argB, argCt"
根据我的测试,如果MyFile2.bat。最终应该使用bat文件的完整路径,这应该可以工作。您可以使用“状态文件”来了解这一点;例如,在MyFile1.bat中,执行以下操作:
echo X > activeProcess.argA
start MyFile2.bat argA, argB, argC
echo X > activeProcess.argX
start MyFile2.bat argX, argY, argZ
:waitForSpawned
if exist activeProcess.* goto waitForSpawned
并在MyFile2.bat的末尾插入此行:
del activeProcess.%1
您还可以在等待周期中插入一个
ping
延迟,以减少此循环中的CPU浪费。您可以这样做:
start MyFile2.bat argA, argB, argC
start MyFile2.bat argX, argY, argZ ^& echo.^>End.val ^& exit
:testEnd
if exist end.val (del end.val
echo Process completed
pause)
>nul PING localhost -n 2 -w 1000
goto:testEnd
当第二个start2.bat finish为job时,将创建一个文件“End.val”,您只需测试该文件是否存在,就知道您的过程已经完成
如果第一个myfile2的执行时间更长,那么第二个myfile2的执行时间也更长(使用另一个文件名),您可以使用第一个启动myfile2.bat
,并在:testend
start MyFile2.bat argA, argB, argC ^& echo.^>End1.val ^& exit
start MyFile2.bat argX, argY, argZ ^& echo.^>End.val ^& exit
:testEnd
if exist end.val if exist end1.val (del end.val
del end1.val
echo Process completed
pause)
>nul PING localhost -n 2 -w 1000
goto:testEnd
简单地使用Start/WAIT参数
start /wait MyFile2.bat argA, argB, argC
start /wait MyFile2.bat argX, argY, argZ
或者将
start
更改为call
,这是我的第一个想法。然而,也许他们希望这两个调用并行运行。在这种情况下,您的建议和我的建议都不合适。+1,但为了确保权限没有问题,最好在%TEMP%
目录中创建标志文件。我可能会在:waitforshawned
和条件之间添加一个小延迟,并且我会在脚本开始处插入del
行(顺便问一下,您是指del activeProcess.
),以防脚本意外中断。