等待并行批处理脚本或命令行 [英] Waiting for Parallel batch scripts or command lines

查看:159
本文介绍了等待并行批处理脚本或命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难在此处修改脚本以满足我的要求: https://stackoverflow.com/a/12665498/4683898

I am finding it difficult to modify the script here to suit my requirements: 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命令(运行一段时间),并且可以与上述脚本很好地工作.

I am using a batch script, not to execute further batch scripts, but simply executing cmd commands (which run for a period of time) in parallel, and that works fine with the above script.

但是,我希望能够同时运行两个以上的命令/脚本,即3、4、5(或任何我想要的)命令,它们可以并行运行,我们称之为x.

However, I want to be able to run more than just 2 commands/scripts, i.e. 3, 4, 5, (or whatever I desire) commands at a single time, running in parallel, let's call it, x.

因此,我想运行x个cmd命令(在 parallel 中执行),等待它们终止(使用/c),然后执行 next 一堆x数量的cmd命令,然后 next 一堆等等,等等,直到所有cmd命令都已执行.

So, I want to run x amount of cmd commands (that are executing in parallel), wait for them to terminate (using /c), and then executing the next bunch of x amount of cmd commands, and then the next bunch, etc, etc, until all cmd commands have executed.

我如何相应地修改该脚本? (尽管在启动批处理脚本中重复出现错误该进程无法访问该文件,因为该文件正在被另一个进程使用.",但我进行了一些尝试;我认为此文件"是指锁定文件.)

How could I modify that script accordingly? (I have made a few attempts albeit with repeating errors "The process cannot access the file because it is being used by another process." in the initiating batch script; I presume this 'file' refers to the lock file.)

谢谢

@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

推荐答案

您可以使用以下脚本轻松调用文件:

You can call the files easily with this script:

@echo off
setlocal
set "lock=%temp%\wait%random%.lock"


call :a one.bat 1
call :a two.bat 2
call :wait
call :a three.bat 1
call :a name.bat 2
call :a gfwagwa.bat 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

call :wait只是替换了等待.每当调用了所有要异步运行的文件时,就调用wait函数.然后,您可以调用更多脚本.

call :wait simply replaces the waiting. Whenever you have called all files that are to be run asynchronously, call the wait function. Then you can call more scripts.

第二个参数是锁定文件的编号.在关闭所有使用它们的脚本之前(即在下一个call :wait之前),确保没有重复的数字.尽管无论如何您都不会用完数字,但没有理由使用重复项.

The second parameter is the number of the lock file. Make sure you don't have duplicate numbers before all those scripts using them are closed (i.e. before the next call :wait). Though you're not going to run out of numbers anyway, no reason to use duplicates.

这篇关于等待并行批处理脚本或命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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