Running multiple batch files on different remote servers in a specific order

て烟熏妆下的殇ゞ 提交于 2019-12-11 14:54:15

问题


I have some batch files which need to be run asynchronously in following order on 6 remote servers located on same network and all servers having common username & password to login. I have put Master.bat file on Server5.

STEP1 :Run file1.bat on 4 different servers (Server1, Server2, Server3, Server4) in parallel           
STEP2 :Run file2.bat on 2 different servers (Server5, Server6) in parallel                                                  
STEP3 :Run file3.bat on Server5 (which is local server in my case as Master.bat is on Server5 only )                                                                                      
STEP4 :Run file4.bat on 2 same servers as in STEP2 (Server5, Server6) in parallel                                   
STEP5 :Run file5.bat on 4 same servers as in STEP1 (Server1, Server2, Server3, Server4) in parallel  

I have planned to do this as follows :

Serverlist1.txt :

Server1 ip
Server2 ip
Server3 ip
Server4 ip

Serverlist2.txt :

Server5 ip
Server6 ip

Master.bat :

@echo off 
setlocal enabledelayedexpansion
for /F "delims= " %i in (C:\test\Serverlist1.txt) do ( psexec \\%i C:\test\file1.bat )
for /F "delims= " %i in (C:\test\Serverlist2.txt) do ( psexec \\%i C:\test\file2.bat )
call file3.bat
for /F "delims= " %i in (C:\test\Serverlist2.txt) do ( psexec \\%i C:\test\file4.bat )
for /F "delims= " %i in (C:\test\Serverlist1.txt) do ( psexec \\%i C:\test\file5.bat )

I havn't tested it yet so not sure whether multiple FOR loops in a Master.bat file will be waiting for each other to be finished or not ?. Should I put these FOR loops in other Batch files and them CALL them in Master.bat ? This all seems bit lengthy to me.Is there any simple method to do this ? Need help !

EDIT1

file1.bat

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

I want file1.bat to wait until the "SUCCESS" signal is received.Where I should put waitfor SUCCESS command in file1.bat file ?

In above Master.bat file where I should insert waitfor /SI SUCCESS command to send a signal to file1.bat to start running ?

Actually I have to define different Signal Name for all the five batch files, sothat I can run them one after another as per 5 steps mentioned above.


回答1:


a. One option is to run all commans on each server successively: eg: bat1 on Server1, S2, S3, S4, bat2 on S5, S6, etc

b. If option a. does not fit to you, I don't see a simple solution:

  1. Create a ServerlistX.txt file for each step (with necessary servers IP).
  2. Set Step=0
  3. call all servers for first step.
  4. Monitor if all server batches completed successfully. Here you can set a specific window name and use tasklist /FI ... command to filter processes by name and window name, and close child windows. Waits between check can be emulated by ping command "ping localhost>nul". It will last ~ 3 sec.
  5. Set Step=Step+1
  6. Check if Step is not exceeding max and exit if so.
  7. Goto step 3.


来源:https://stackoverflow.com/questions/19961541/running-multiple-batch-files-on-different-remote-servers-in-a-specific-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!