在被调用和调用批处理文件中插入'waitfor'命令

时间:2013-11-16 09:48:18

标签: batch-file wait remote-access psexec

我有3个被称为批处理文件,如called1.bat:

called1.bat

taskkill /f /im firefox.exe
net stop W3SVC
sc config W3SVC start= demand
net start W3SVC

和一个调用Master.bat文件,它调用所有三个被调用的批处理文件,如下所示:

Master.bat

@echo off 
setlocal enabledelayedexpansion
REM 1st for loop:
for /F "delims= " %i in (C:\test\Serverlist1.txt) do ( psexec \\%i start C:\data\called1.bat )
REM 2nd for loop:
for /F "delims= " %i in (C:\test\Serverlist2.txt) do ( psexec \\%i start C:\data\called2.bat )
REM 3rd for loop:
for /F "delims= " %i in (C:\test\Serverlist3.txt) do ( psexec \\%i start C:\data\called3.bat )

实际上我想从Master.bat文件向三个called.bat文件发送不同的信号,以便在一些远程服务器上运行它们,如下所示:

called1.bat file to wait until the "SIGNAL1" is received from Master.bat
called2.bat file to wait until the "SIGNAL2" is received from Master.bat
called3.bat file to wait until the "SIGNAL3" is received from Master.bat

我应该将waitfor SIGNAL1命令放在 called1.bat 文件中?

我应该在{strong> Master.bat 文件中放置waitfor /SI SIGNAL1命令,将信号发送到called1.bat以开始运行?和其他文件类似..

我的主要目的是:在Master.bat文件中,2nd for loop只应在1st for loop完成其完整执行后开始运行,同样3rd for loop在{ {1}}这就是为什么到目前为止更喜欢2nd for loop方法的原因。

1 个答案:

答案 0 :(得分:1)

基本理念可以是:

在master.bat

for each server in list (
    create the remote process
    spawn a local copy of waitfor to know when remote ends
)
send signal to remote systems to start work
loop waiting for local waitfors to end on signals from remote servers

waitfor的本地副本应该生成为“start”“/ b waitfor remoteserversignal”,每个服务器都有唯一的远程服务器信号,所以我们知道它何时完成

tasklist / pslist / wmic可用于了解等待的本地副本何时完成。

在remote.bat

wait for master signal to start
do work
send signal to master to signal end of work