Synchronizing batch scripts execution

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Synchronizing batch scripts execution

#1 Post by SenHu » 16 Dec 2009 14:16

I was asked this question. I am posting an answer here in case others have this same question and are looking for a solution.


Here is the original question (reworded).

I administer several computers. I have to run a batch script on each one of them. This batch script is in file C:\EODProcessing.bat. (EOD=End of Day)

The script has to run on each server in a specific sequence. For example, the script must run on server1 before server2. I have scheduled these servers to run the script at 2:00 am, 2:05 am, 2:10 am, etc. But that's not working, because sometimes the script on server1 is not completed running within its allotted time of 5 minutes.

How can I synchronize this process ?


Here is the answer.

On each server, create a file C:\BatchDependency.txt. It will list the severs - one per line - that must have completed running the script before we start the script on this server. For example, this file on server2 would contain server1.

We will save the running status of each server in a shared folder, in file Z:\SynchronizationStatus.txt.

Now, instead of scheduling the batch script "C:\EODProcessing.bat" in the task scheduler, schedule to run the following script instead.

Code: Select all

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Script C:\Synchronize.txt
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var str serverlist, server, status
var bool ready

# Collect the list of servers that we are dependent on.
cat "C:\BatchDependency.txt" > $serverlist

set $ready = false

while ( NOT ( $ready) )
do

    var bool ranall
    set $ranall = true

    # Read the current synchronization status.
    cat "Z:\SynchronizationStatus.txt" > $status

    # Go thru the list of dependent servers one by one. Each of these
    # servers must have the corresponding line "ran <server>" in file
    # Z:\SynchronizationStatus.txt.
    lex "1" $serverlist > $server
    while ( ($server <> "") AND ($ranall) )
    do

        # Has $server completed running ?
        if ( { sen ("^ran "+$server+"^") $status } <= 0 )
            set $ranall = false
        endif

        # Get the next server from the dependency list.
        lex "1" $serverlist > $server
    done # while ( ($server <> "") AND ($ranall) ) - do

    # We have checked all dependency server. If $ranall is true
    # at this point, the batch script "C:\EODProcessing.bat" has run
    # on all servers we are dependent on.
    if ( $ranall )
    do
        # We are ready to run.
        set $ready = true

        # Run the batch script "C:\EODProcessing.bat".
        system "C:\EODProcessing.bat"

        # Since now we are done running, add our entry in the status
        # file "Z:\SynchronizationStatus.txt" .
        echo "ran " getcomputer() "\n" >> "Z:\SynchronizationStatus.txt"
    done
    else
        # We are not ready to run yet.
        sleep 10        # Sleeep for 10 seconds.
    endif
done # while ( NOT ( $ready) ) - do
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# End of script
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>





Copy and paste the above script in file "C:\EODProcessing.bat. Schedule the "C:\Synchronize.txt" script in task scheduler with the following command.

Code: Select all

"C:\biterScripting\biterScripting.exe" "C:\Synchronize.txt"


The script I wrote ("C:\Synchronize.txt") is in biterscripting language - documentation at http://www.biterscripting.com . You can translate it to any language of your choice.

Post Reply