How to sleep or wait in Windows batch file ?


👤 Diwas Poudel    🕒 Nov 13 2021    📁 Fix    📜 0 comment

The Sleep/Wait Command is a very useful command that is used to pause for a set amount of time while a batch script is being executed. To put it another way, this command makes it easier to run a command for a set amount of time. You may find yourself in a situation where you want to run a series of command lines one after the other, but with a time delay between them.

In this case, the sleep command will come in handy. This article will show you how to use the sleep command in Windows batch scripts.

Because Windows does not have a direct Sleep command as provided in Linux, we must use a different method to achieve the same result. Furthermore, no root privileges are required to implement in a batch script.

1) Using timeout to achieve sleep function

timeout is an old Windows command introduced in Windows 2000 that is used to add a delay between two tasks.

Usage :

timeout /t <timeoutinseconds> [/nobreak]

Here,

/t <timeoutinseconds> Specify the number of seconds to wait (between -1 and 99999) before the command processor continues processing. When you enter the value -1, the computer will wait indefinitely for a keystroke. Also, a number of seconds in a nonwhole number like 1.5 is not accepted.
/nobreak Specifies to ignore user keystrokes. But accepts CTRL + C strokes which directly terminate the batch.
/? Displays help at the command prompt.
timeout-help
fig. timeout help

 

Example1: Basic timeout in Batch Script

@echo off
echo First Task Here ...
echo Approximately 10-second delay
timeout /t 10 
echo Second Task Here ...
pause

We have two tasks here, and we have inserted a timeout script that waits for 10 seconds for the First Task to complete before starting the Second Task.

Drawback: This will show unnecessary "Waiting for 0 seconds, press a key to continue..." and when you press any key it will continue to the next task. It is a good way of letting the user know the safe escape route to skip sleep.

Example2: Timeout with only CTRL + C key to Skip

@echo off
 .....
timeout /t 10 /nobreak
.....
pause

Here, /nobreak does not allow to break or skip sleep with any key. But pressing the CTRL + C key combination will force to close the batch processing and close the program immediately.

Example 3: Sleep without timeout message

@echo off
....
timeout /t 15 /nobreak > nul
....
pause

Appending >nul part so that command does not display output anything to screen. It suppresses the countdown time.

Note that: Ctrl + C will break the whole program.

Drawbacks of TIMEOUT 

Here are some drawbacks of timeout :

  • Cannot perform delay in a fraction of a second. ie. like timeout /t 0.5 does not work. And will give
    an error message stating "Error: Invalid value for timeout (/T) specified. Valid range is -1 to 99999)
  • Cannot use a second value greater than 99999

2) Using Ping to Delay to achieve sleep function

@echo off
.....
ping localhost -n 10 > nul
.....
pause

Ping is actually used to determine whether or not a resource responds to a request. Ping sends a request to the resource and waits one second for a response. As a result, it is useful to use it as a delay function. Even if it waits for one second, it may receive a response in a few milliseconds. However, as soon as you ping the last packet here in the tenth request, it will automatically stop.

The resource we are pinging here is localhost, which is a local loopback, and it responds instantly within a millisecond but must wait 1 second each. And here in the example, we are waiting for nearly 10 seconds before executing the next line.

Note that: Ctrl + C will break delay.

Using Ping to Delay with nonexisting IP

@echo off
....
ping 192.4.4.4 -n 1 -w 10000 > nul
....
pause

Here waiting for 10 seconds. Here we must ping to a non-existing IP address and if it exists it will not work. -w indicates waiting and here it waits for 10000 ms, you can change this value. And by default ping will request 4 times to the resource. Here, -n 1 indicates request only 1 time.

Using Ping to Delay in less than 1 second.

Just replace 10000ms with anythings smaller than 1000ms. like 500ms as shown below

ping 192.4.4.4 -n 1 -w 500 > nul