并行执行批处理文件,并从每个文件中获取退出代码 [英] execute batch files in parallel and get exit code from each

查看:104
本文介绍了并行执行批处理文件,并从每个文件中获取退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一个批处理文件并行执行一组批处理文件,并从每个批处理文件中获取退出代码.当我使用start时,它会并行执行批处理文件(新的cmd窗口),但不会返回每个文件的退出代码.在使用call时,我可以获得退出代码,但是批处理文件的执行顺序发生.我有以下代码:

How can I execute set of batch files from single batch file in parallel and get the exit code from each. When I use start it executes the batch file in parallel (new cmd window) but don't return the exit code from each. And while using call, I can get the exit code but the batch file execution happens sequentially. I have following code:

ECHO ON
setlocal EnableDelayedExpansion
sqlcmd -S server_name -E  -i select_code.sql  >\path\output.txt

for /f "skip=2 tokens=1-3 delims= " %%a in ('findstr /v /c:"-"  \path\output.txt') do (

echo $$src=%%a>\path1\%%c.prm
echo $$trg=%%b>>\path1\%%c.prm
set param_name=%%c

start cmd \k  \path\exec_pmcmd_ctrm.bat workflow_name  %%param_name%%
ping 1.1.1.1 -n 1 -w 5000 > nul
set exitcode="%V_EXITCODE%"
echo %exitcode%>>\path\exitcode.txt
)   

这使用不同的变量并行执行exec_pmcmd_ctrm.bat 3次,但是我无法从每次执行中获取退出代码.我尝试使用调用,但随后我错过了bat文件的并行执行.在这方面有帮助吗?

This executes the exec_pmcmd_ctrm.bat 3 times with different variable in parallel , but I am unable to get the exit code from each execution. I tried using call but then I miss the parallel execution of bat file. Any help in this regard?

推荐答案

首先,另一个批处理文件(以exit /B value命令结尾)中的退出代码"是通过%ERRORLEVEL%变量(不是).另外,如果这样的值在 循环内更改,则必须通过延迟扩展"代替:!ERRORLEVEL!(和程序开始处的EnableDelayedExpansion).但是,这些观点并不能解决您的问题,因为这里存在误解...

First of all, the "exit code" from another Batch file (that ends with exit /B value command) is taken via %ERRORLEVEL% variable (not %V_EXITCODE%). Also, if such a value changes inside a FOR loop, it must be taken via Delayed Expansion instead: !ERRORLEVEL! (and EnableDelayedExpansion at beginning of your program). However, these points don't solve your problem because there is a misconception here...

使用START命令(不带/WAIT开关)时, parallel cmd.exe进程开始执行.这意味着,第一个批处理文件没有直接方式 可以知道并行批处理在哪个时刻结束,以便在此时获取其ERRORLEVEL!没有等待启动的批处理文件"命令,但是即使该命令存在,也不能解决具有几个并发批处理文件的问题.换句话说,您的问题无法通过直接命令解决,因此必须解决.

When START command is used (without the /WAIT switch), a parallel cmd.exe process start execution. This means that there is not a direct way that the first Batch file could know in which moment the parallel Batch ends in order to get its ERRORLEVEL at that point! There is not a "wait for a started Batch file" command, but even if it would exist, it don't solve the problem of have several concurrent Batch files. In other words, your problem can not be solved via direct commands, so a work around is necessary.

最简单的解决方案是并行批处理文件将其ERRORLEVEL值存储在一个文件中,以后可由原始批处理文件读取.这样做暗示着同步问题,以避免同时写入同一文件,但这又是另一回事了……

The simplest solution is that the parallel Batch files store their ERRORLEVEL values in a file that could be later read by the original Batch file. Doing that imply a synchronization problem in order to avoid simultaneous write access to the same file, but that is another story...

这篇关于并行执行批处理文件,并从每个文件中获取退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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