简体   繁体   English

如何从具有依赖关系的一个批处理文件启动多个批处理文件?

[英]How do I launch multiple batch files from one batch file with dependency?

I want to run one batch file, that start the other batch files. 我想运行一个批处理文件,启动其他批处理文件。 I looked at a similar question posted here: How to run multiple .BAT files within a .BAT file 我查看了此处发布的类似问题: 如何在.BAT文件中运行多个.BAT文件

I followed the example (specifically the very last suggestion) and it worked...partially. 我按照这个例子(特别是最后一个建议)而且它有效...部分。 It did launch the batch files that I needed to. 它确实启动了我需要的批处理文件。 However, in order for the applications to function properly, some of these batch files have to open, and then run their course for a few seconds, before the next the next batch file launches, otherwise they won't be registered. 但是,为了使应用程序正常运行,这些批处理文件中的一些必须打开,然后在下一个批处理文件的下一个启动之前运行它们的过程几秒钟,否则它们将不会被注册。 Specifically, the first batch file launches a web applications server (JBOSS 5.1), then the next batch file opens a pool manager, then the other two launch distribution servers. 具体来说,第一个批处理文件启动Web应用程序服务器(JBOSS 5.1),然后下一个批处理文件打开池管理器,然后另外两个启动分发服务器。 When I run my batch file that calls the others, they all launch nearly simultaneously, and they do not register each other. 当我运行调用其他文件的批处理文件时,它们几乎同时启动,并且它们不会相互注册。 Can I even do this with a batch file? 我甚至可以使用批处理文件吗? Or do I have to go into the code of the other batch files and make changes there? 或者我是否必须进入其他批处理文件的代码并在那里进行更改? I want to avoid that at all costs. 我想不惜一切代价避免这种情况。

Here is what I have so far: 这是我到目前为止:

start cmd /k CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat

start cmd /k CALL batch1.bat

start cmd /k CALL batch2.bat

start cmd /k CALL batch3.bat

You can drop the start cmd /k and just use CALL . 您可以删除start cmd /k并使用CALL

CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat
CALL batch1.bat
CALL batch2.bat
CALL batch3.bat

Answer: 回答:

Add the /wait option to the start command. /wait选项添加到start命令。

WAIT        Start application and wait for it to terminate.

Example: 例:

start /wait cmd /k CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat

start /wait cmd /k CALL batch1.bat

start /wait cmd /k CALL batch2.bat

start /wait cmd /k CALL batch3.bat

Otherwise just use a ping delay between the starts. 否则只需在启动之间使用ping延迟。 (See user706837's Answer) (见user706837的答案)

References: 参考文献:

Technet , Rob , SS64 , DosTips TechnetRobSS64DosTips

Whenever I have batch files that depend on another I either: 1. nest them; 每当我有依赖另一个的批处理文件时,我要么:1。嵌套它们; meaning, if batch1 needs to run before batch2, then I add batch2 within batch1. 意思是,如果batch1需要在batch2之前运行,那么我在batch1中添加batch2。 2. put a 'sleep' call within batch2. 2.在batch2中进行“睡眠”调用。 This is only possible if you are fairly certain of the startup duration for batch1. 只有在您确定batch1的启动持续时间时才可以执行此操作。

A sample sleep command is: 示例sleep命令是:

ping 127.0.0.1 -n 4 > null

This will make the batch file wait for 3 seconds. 这将使批处理文件等待3秒。 (Because there are only 3, 1 second sleeps, between each of the 4 echos) (因为在4个回声中每个回声之间只有3秒睡眠)

Examples: 例子:

start cmd /k CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat
ping 127.0.0.1 -n 4 > null
start cmd /k CALL batch1.bat
ping 127.0.0.1 -n 4 > null
start cmd /k CALL batch2.bat
ping 127.0.0.1 -n 4 > null
start cmd /k CALL batch3.bat
There are multiple ways for that.

1.

rem echo call A

CALL a.bat

rem echo call B

CALL b.bat

rem echo call C

CALL c.bat

rem pause

--------------------

2.

echo call A

start cmd /k CALL a.bat


echo call B

start cmd /k CALL b.bat

echo call C

start cmd /k CALL c.bat

pause

---------------------

Here the difference is-

start cmd /k

   It creates these many instances. So we can see multiple number of CMD prompts.


CALL

   Each descendent CALL waits for the completion of the previous CALL.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM