Add a Time Delay to a Batch Script

I still do much of my scripting using CMD in batch files.  I’ve frequently ran into instances when I need to add a time delay to a batch before it continues terminating.

I’ve seen a variety of reasons for this, but the most useful to me is when building SCCM applications that need to call EXE installers.  SCCM’s detection rules will sometimes try and detect a program before an EXE install finishes thus making Software Center report the install failed even though the program does indeed finish installing after a few more seconds.  Adding a short time delay to the installer can help avoid this.

To add a time delay, simply use the PING command to issue a NUL ping to the loopback address:

PING -n 5 127.0.0.1 > NUL

The -n option allows you to specify the number of echo requests to send.  The delay between each ping is 1 second, so for a delay of 10 seconds ping 11 times:

PING -n 11 127.0.0.1 > NUL

Of course there are other ways to accomplish this same thing.

Two similar methods are to use either SLEEP.EXE or TIMEOUT.EXE.  The main advantage of using PING over these methods is that PING will consume less processor time.

Another option I’ve experimented with is using the START command with the /WAIT option to call an EXE.  This is supposed to start a program and then wait for it to fully terminate before continuing, but in my experience it doesn’t always wait…  And of course I have no time for inconsistency!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: