Search code examples
batch-filewaitremote-accesspsexec

Inserting 'waitfor' command in called and calling batch files


I have 3 called batch files like called1.bat :

called1.bat

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

and a calling Master.bat file which calles all the three called batch files as below:

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 )

Actually I want to send different signals from Master.bat file to the three called.bat files to run them on some remote servers as below:

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

Where I should put waitfor SIGNAL1 command in called1.bat file ?

Where I should put waitfor /SI SIGNAL1 command in Master.bat file to send the signal to called1.bat to start running ? and similarly for other files..

My prime purpose here is: In Master.bat file, 2nd for loop should only start running once 1st for loop completes its full execution and similarly 3rd for loop after 2nd for loopthats why preferring waitfor SIGNAL method so far.


Solution

  • The basic idea can be:

    In 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
    

    local copies of waitfor should be spawned as `start "" /b waitfor remoteserversignal" with remote server signal unique for each server, so we know when it has finished

    tasklist / pslist / wmic can be used to know when local copies of wait for have finished.

    In remote.bat

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