How to Sleep or Wait for a Specific Amount of Time in Batch Script
Creating a pause or a "sleep" in a script is a fundamental technique for controlling the flow of execution. You might need to wait for a network resource to become available, give a slow application time to initialize, or simply pace a loop to avoid overwhelming a system with requests. While batch scripting has a legacy "trick" for this, modern Windows includes a dedicated, built-in command that is far more reliable and easy to use: TIMEOUT.
This guide will teach you how to use the modern TIMEOUT command to pause your script for a specific number of seconds. You will also learn about the classic PING workaround and understand why TIMEOUT is the superior choice for nearly all situations.
The Modern Method (Recommended): TIMEOUT
The TIMEOUT command is the official, built-in utility for pausing a batch script. It waits for a specified number of seconds or until a key is pressed. It is the cleanest, most reliable, and recommended method for creating a delay.
Syntax: TIMEOUT /T seconds
/T <seconds>: The Timeout period in seconds. The valid range is from -1 to 99999. A value of -1 means wait indefinitely for a keypress.
The Classic "Trick": Using PING
Before the TIMEOUT command was introduced, the standard way to create a delay was a clever but imprecise workaround using the PING command. The logic was to ping the local machine's loopback address (127.0.0.1), which responds instantly. Since each ping waits about 1 second for a reply, you could create a delay by sending a specific number of pings.
Syntax: PING -n <seconds+1> 127.0.0.1 > NUL
-n <count>: The number of echo requests to send. You need to useseconds + 1because the first ping is sent immediately. To wait 5 seconds, you need 6 pings.> NUL: This is essential to hide the output of thePINGcommand.
Why TIMEOUT is better: The PING trick is a hack. It's not guaranteed to be exactly one second per ping, and its intent is not as clear as TIMEOUT. It should only be used if you need a script to be compatible with very old Windows versions (like XP).
Basic Example: A Simple 5-Second Pause
This script demonstrates a simple pause between two actions using the recommended TIMEOUT command.
Example:
@ECHO OFF
ECHO Starting process...
ECHO This will take a moment. The next step will begin in 5 seconds.
REM --- This is the sleep command ---
TIMEOUT /T 5
ECHO.
ECHO Resuming process...
In the output, the script will print the first messages, then display a countdown and wait.
Starting process...
This will take a moment. The next step will begin in 5 seconds.
Waiting for 5 seconds, press a key to continue ... 3
After 5 seconds (or if a key is pressed), the script will continue.
Key TIMEOUT parameters explained:
/T <seconds>: (Required) The number of seconds to wait./NOBREAK: This is a very important switch. By default, any keypress will interrupt the timeout./NOBREAKtells the command to only be interrupted by Ctrl+C. This is crucial if you need to guarantee a minimum wait time and don't want an accidental keypress to continue the script early.> NUL: While not a parameter ofTIMEOUTitself, this redirection is used to suppress the countdown output, making your script run silently. This is essential for clean, non-interactive scripts.
Common Pitfalls and How to Solve Them
Problem: The Script Continues if a Key is Pressed
By default, TIMEOUT is designed to be user-interruptible. This can be a problem in an automated script that requires a guaranteed delay (e.g., waiting for a service to start).
Solution: Use /NOBREAK
The /NOBREAK switch makes the pause more robust.
REM This pause cannot be skipped by a normal keypress.
TIMEOUT /T 10 /NOBREAK
Problem: The Countdown Message is Unwanted
In a script that is supposed to run silently in the background, the "Waiting for..." message is undesirable clutter.
Solution: Redirect Output to NUL
This is the standard way to silence a command in a batch script.
REM This command waits for 10 seconds with no output.
TIMEOUT /T 10 /NOBREAK > NUL
This is the most common and recommended syntax for a non-interactive, guaranteed sleep in a modern batch script.
Practical Example: A Polling Loop
A perfect use case for TIMEOUT is in a polling loop. This script checks for the existence of a file, and if it's not found, it waits for 10 seconds before trying again. This avoids a "tight loop" that would consume 100% of a CPU core.
@ECHO OFF
SET "FLAG_FILE=C:\AppData\process_complete.flag"
ECHO --- Waiting for process to complete ---
:CheckLoop
ECHO Checking for flag file at %TIME%...
IF EXIST "%FLAG_FILE%" (
ECHO [SUCCESS] Flag file found!
GOTO :End
)
ECHO File not found. Waiting 10 seconds before re-check.
REM Use the robust, silent syntax for the pause.
TIMEOUT /T 10 /NOBREAK > NUL
GOTO :CheckLoop
:End
ECHO --- Script finished ---
Conclusion
Creating a pause is a fundamental part of controlling the flow and timing of a batch script.
- The modern and recommended method is the
TIMEOUTcommand. It is clear, reliable, and flexible. - The classic
PINGtrick is a legacy workaround that is less precise and should only be used for backward compatibility with very old systems. - For robust, non-interactive scripts, the best syntax is
TIMEOUT /T <seconds> /NOBREAK > NUL. This provides a guaranteed, silent delay.
By using TIMEOUT correctly, you can write smarter scripts that can gracefully wait for other processes, pace themselves, or react to events in a controlled manner.