【发布时间】:2013-12-18 09:13:36
【问题描述】:
MyFile1.bat 调用 MyFile2.bat 两次:
start MyFile2.bat argA, argB, argC
start MyFile2.bat argX, argY, argZ
此时,我如何才能等到调用MyFile2.bat 产生的两个进程都完成?
【问题讨论】:
标签: 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 产生的两个进程都完成?
【问题讨论】:
标签: batch-file command-line command-prompt windows-shell
简单地使用 Start /WAIT 参数。
start /wait MyFile2.bat argA, argB, argC
start /wait MyFile2.bat argX, argY, argZ
【讨论】:
start改成call,这是我的第一个想法。但是,也许他们希望这两个调用并行运行。在这种情况下,你的建议和我的建议都不合适。
你可以这样做:
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 完成时,会创建一个文件“End.val”,你只需要测试这个文件是否存在,那么你就知道你的过程已经完成了。
如果第一个 myfile2 可能需要更多时间来执行,那么第二个你可以对第一个 start 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
【讨论】:
您可以使用“状态文件”来了解这一点;例如,在 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。
【讨论】:
%TEMP% 目录中创建标志文件可能是个好主意。我可能会在:waitForSpawned 和条件之间添加一个小延迟,并且我会在 beginning 的脚本,以防脚本意外中断。
start /w cmd /c "start cmd /c MyFile2.bat argA, argB, argC & start cmd /c MyFile2.bat argA, argB, argCt"
根据我的测试,如果 MyFile2.bat 应该可以工作。最终应该使用 bat 文件的完整路径。
【讨论】: