Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

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

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:	estServerlist1.txt) do ( psexec \%i C:	estfile1.bat )
for /F "delims= " %i in (C:	estServerlist2.txt) do ( psexec \%i C:	estfile2.bat )
call file3.bat
for /F "delims= " %i in (C:	estServerlist2.txt) do ( psexec \%i C:	estfile4.bat )
for /F "delims= " %i in (C:	estServerlist1.txt) do ( psexec \%i C:	estfile5.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...