How to sleep for five seconds in a batch file/cmd
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The standard way to pause execution for five seconds in a Windows batch file is timeout /t 5. This command is available on Windows Vista and later, covers the vast majority of use cases, and requires no extra tools. For older systems or specific edge cases, alternatives like ping, PowerShell's Start-Sleep, or the Windows Resource Kit's sleep.exe exist.
timeout: The Standard Answer
timeout is a built-in Windows command designed specifically for delays:
Flags explained
| Flag | Purpose |
/t 5 | Wait for 5 seconds. Accepts values from 0 to 99999. |
/nobreak | Ignore key presses during the wait. Without this, any key press cancels the delay. |
> nul | Suppress the countdown message ("Waiting for 5 seconds, press a key to continue..."). |
Showing the countdown
If you want the user to see a visual countdown, remove the > nul redirect:
Output:
Variable delay
You can parameterize the delay using a variable:
Or accept it as a command-line argument:
ping: The Classic Workaround
Before timeout was available (Windows XP and earlier), developers used ping to create delays. The technique exploits the one-second interval between ICMP echo requests:
The count is 6, not 5, because the first ping fires immediately and subsequent pings wait one second each. Six pings produce five one-second intervals.
Alternative: Unreachable address with timeout
Another ping variant uses an unreachable address with a millisecond timeout:
192.0.2.1 is part of the TEST-NET-1 range (RFC 5737) and is not routable, so the ping waits for the full 5000ms timeout before returning. This gives a more accurate delay than the multi-ping approach.
Why ping is a workaround, not a solution
The ping method has real drawbacks:
- Timing is approximate, not exact.
- It generates network traffic (even to loopback).
- The
-wvariant depends on the address being unreachable, which can vary on unusual network configurations. - It is confusing to anyone reading the script who does not already know the trick.
Use ping only when timeout is genuinely unavailable.
PowerShell Start-Sleep: Precise and Readable
If PowerShell is available (it is on every modern Windows installation), you can invoke it from a batch file:
For sub-second precision:
The downside is process creation overhead. Launching PowerShell takes 200-500ms, which adds latency beyond the requested sleep time. For a script that calls this once, it does not matter. For a script that loops with frequent short delays, the overhead adds up.
Inline PowerShell for conditional delays
This delays 5 minutes outside business hours and 5 seconds during them. Once you are writing conditional logic inside the delay, consider whether the entire script should be PowerShell rather than batch.
Comparison of All Methods
| Method | Availability | Precision | User-interruptible | Overhead |
timeout /t 5 /nobreak | Windows Vista+ | 1 second | No (with /nobreak) | None |
timeout /t 5 | Windows Vista+ | 1 second | Yes (any key cancels) | None |
ping 127.0.0.1 -n 6 | All Windows | Approximate | No | Minimal |
ping 192.0.2.1 -n 1 -w 5000 | All Windows | Approximate | No | Minimal |
powershell Start-Sleep | Windows 7+ (PS installed) | Millisecond | No | 200-500ms startup |
pause vs timeout: They Solve Different Problems
pause and timeout are frequently confused, but they serve entirely different purposes:
pause blocks until the user presses a key. timeout blocks for a fixed duration. If your script should proceed automatically after a delay, use timeout. If it should wait for human confirmation, use pause.
Practical Patterns
Retry with backoff
This pattern increases the wait time on each retry (1s, 2s, 3s, etc.), giving the service progressively more time to start.
Countdown with progress
Output:
Wait for a file instead of a fixed delay
Fixed delays are often a code smell. If you are waiting for a process to produce output, poll for the result instead:
This finishes as soon as the file appears rather than waiting for a worst-case fixed duration.
Common Pitfalls
Using pause when you need an automatic delay. pause waits for user input indefinitely. In automated scripts, CI/CD pipelines, or scheduled tasks, pause hangs forever. Use timeout for time-based delays.
Forgetting /nobreak and getting unexpected early returns. Without /nobreak, any key press cancels the timeout. In unattended scripts, a stray input event can skip the delay entirely.
Using ping -n 5 instead of ping -n 6. The first ping fires immediately, so five pings produce only four one-second intervals (about 4 seconds, not 5). Add one to the count.
Relying on fixed delays instead of polling. A 5-second sleep works until the operation takes 6 seconds. Polling for the actual condition (file exists, port responds, process exited) is more robust.
Calling PowerShell in tight loops. Each powershell -Command invocation starts a new process with 200-500ms overhead. In a loop that runs hundreds of times, this overhead dominates. Use timeout for simple in-loop delays.
Mixing up seconds and milliseconds. timeout uses seconds. ping -w uses milliseconds. PowerShell Start-Sleep -Seconds uses seconds, but Start-Sleep -Milliseconds uses milliseconds. Getting these units wrong produces delays that are 1000x too short or too long.
Summary
timeout /t 5 /nobreak > nulis the standard, preferred way to sleep in modern batch files.pingis a legacy workaround for systems withouttimeout. Use-n 6for a 5-second delay (not-n 5).- PowerShell
Start-Sleepprovides millisecond precision but adds process startup overhead. - Use
pauseonly for interactive user confirmation, never for timed delays in automated scripts. - Prefer polling for conditions over fixed delays when waiting for external processes.
- Always include
/nobreakin unattended scripts to prevent accidental early cancellation.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.