【发布时间】:2017-11-22 21:46:05
【问题描述】:
我发现很难在此处修改脚本以满足我的要求: https://stackoverflow.com/a/12665498/4683898
@echo off
setlocal
set "lock=%temp%\wait%random%.lock"
:: Launch one and two asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" cmd /c 9>"%lock%1" one.bat
start "" cmd /c 9>"%lock%2" two.bat
:Wait for both scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (1 2) do (
( rem
) 9>"%lock%%%N" || goto :Wait
) 2>nul
::delete the lock files
del "%lock%*"
:: Launch three and four asynchronously
start "" cmd /c three.bat
start "" cmd /c four.bat
我使用的是批处理脚本,不是为了执行进一步的批处理脚本,而是简单地并行执行 cmd 命令(运行一段时间),并且与上面的脚本配合得很好。
但是,我希望能够同时运行 2 个以上的命令/脚本,即 3、4、5(或任何我想要的)命令,并行运行,我们称之为 x。
所以,我想运行 x 个 cmd 命令(在 并行 中执行),等待它们终止(使用 /c),然后执行 next 一堆 x 数量的 cmd 命令,然后是 next 一堆,等等,直到所有的 cmd 命令都执行完毕。
如何相应地修改该脚本? (我已经做了一些尝试,尽管在启动批处理脚本中重复出现错误“该进程无法访问该文件,因为它正在被另一个进程使用。”我认为这个“文件”是指锁定文件。)
谢谢
编辑:
@echo off
setlocal
set "lock=%temp%\wait%random%.lock"
call :a start cmd.exe /c somecommandA 1
call :a start cmd.exe /c somecommandB 2
call :wait
call :a start cmd.exe /c somecommandC 1
call :a start cmd.exe /c somecommandD 2
call :a start cmd.exe /c somecommandE 3
exit /b
:a
start "%~2" cmd /c 9>"%lock%%2" %1
exit /b
:wait
1>nul 2>nul ping /n 2 ::1
for %%N in (%lock%*) do (
( rem
) 9>"%%N" || goto :Wait
) 2>nul
【问题讨论】:
标签: batch-file cmd parallel-processing