TechFRP Search

Custom Search

Wednesday, January 5, 2011

How to Create Delays in Batch file Execution

     Happy New Year. It's been a great holiday but it's time to get back to work. I had a need recently to create a delay in the execution of a batch file that I run on one of my servers. The batch file stops and then restarts a service nightly. Sometimes the stop command takes a little time before it actually stops the service and can conflict with the start command since the service isn't in a state ready to restart. For those that don't already know, batch files are sequentially executed command statements. Over the years they have added more complex functionality to batch files but in the end they are still a sequential process. They can be very powerful but are made up of very simple statements. As a result there is no easy command like a count() function, but there is a trick you can use instead.

      At the point in your batch file where you want execution to delay before continuing simply enter:

      ping localhost -n 60

      This will result in the system pinging the localhost PC for a count of 60 resulting in roughly a 60 second delay in your Batch files execution before continuing. The end result is a successful execution delay. You can modify the count number to anything you need to create the length of delay required.

      For me, this was perfect to create a delay for 60 seconds before the batch file attempted to restart the service. Since implementing this line in the the batch file it hasn't failed once to properly stop and restart the service.

      It's a simple little trick that really helps customize your batch files. I hope you found this tip helpful. If you did, let me know, leave a comment or give me a shout on Twitter. @wjgtech. 

2 comments:

  1. That trick was just what I needed, thanks :-)

    ReplyDelete