同时执行多个批处理文件并监控它们的进程是否完成 [英] Execute multiple batch files concurrently and monitor if their process is completed

查看:22
本文介绍了同时执行多个批处理文件并监控它们的进程是否完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用多个批处理文件的主批处理文件.我希望能够同时执行所有这些批处理文件.全部完成后,我需要在主批处理文件中进行进一步的处理.

I have a main batch file which calls multiple batch files. I want to be able to execute all these batch files at the same time. Once they are all done, I have further processes that needs to carry on in the main batch file.

当我使用开始"调用多个批处理文件时,我可以同时启动所有批处理文件,但我无法跟踪它们.(主批处理文件在执行其他批处理文件时认为它们的进程已经完成).

When I use 'Start' to call the multiple batch files, I'm able to kick off all batch files concurrently but I lose tracking of them. (Main batch file thinks their processes are done the moment it executes other batch files).

当我使用Call"时,我能够监控批处理文件的进程,但它会按顺序启动批处理文件,而不是同时启动.

When I use 'Call', I'm able to monitor the batch file process, but it kicks off the batch files sequentially instead of concurrently.

有没有办法解决这个问题?我对这台电脑的权限有限,我正在尝试仅使用 Batch 来完成此操作.

Is there a way around this? I have limited permissions on this PC and I'm trying to accomplish this using Batch only.

主批处理文件

call first.bat
call second.bat
call third.bat
:: echo only after all batch process done
echo done!

第一个.bat

timeout /t 10

第二个.bat

timeout /t 10

第三个.bat

timeout /t 10

推荐答案

这是解决这个问题最简单、最有效的方法:

This is the simplest and most efficient way to solve this problem:

(
start first.bat
start second.bat
start third.bat
) | pause

echo done!

在这个方法中,主文件中的等待状态是事件驱动,所以它不消耗任何CPU时间.pause 命令将在 ( block ) 中的任何命令输出一个字符时终止,但 start 命令不显示任何输出 在这个 cmd.exe 中.这样,pause 会一直等待一个字符,直到所有由 start 命令启动的进程结束.此时,与 ( block ) 关联的管道关闭,因此 pause Stdin 关闭,命令由 cmd.exe 终止>.

In this method the waiting state in the main file is event driven, so it does not consume any CPU time. The pause command would terminate when anyone of the commands in the ( block ) outputs a character, but start commands don't show any output in this cmd.exe. In this way, pause keeps waiting for a char until all processes started by start commands ends. At that point the pipe line associated to the ( block ) is closed, so the pause Stdin is closed and the command is terminated by cmd.exe.

这篇关于同时执行多个批处理文件并监控它们的进程是否完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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