Batch File
CMD
Programming
Sleep Function
Coding Tips

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.

Browse interview questions

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:

bat
1@echo off
2echo Starting process...
3timeout /t 5 /nobreak > nul
4echo Process resumed after 5 seconds.

Flags explained

FlagPurpose
/t 5Wait for 5 seconds. Accepts values from 0 to 99999.
/nobreakIgnore key presses during the wait. Without this, any key press cancels the delay.
> nulSuppress 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:

bat
1@echo off
2echo Preparing deployment...
3timeout /t 5 /nobreak
4echo Deploying now.

Output:

text
Preparing deployment...
Waiting for 5 seconds, press CTRL+C to quit ...
Deploying now.

Variable delay

You can parameterize the delay using a variable:

bat
1@echo off
2set WAIT_SECONDS=5
3echo Waiting %WAIT_SECONDS% seconds...
4timeout /t %WAIT_SECONDS% /nobreak > nul
5echo Done.

Or accept it as a command-line argument:

bat
1@echo off
2if "%1"=="" (
3    echo Usage: delay.bat [seconds]
4    exit /b 1
5)
6timeout /t %1 /nobreak > nul
7echo Waited %1 seconds.

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:

bat
1@echo off
2echo Waiting for about 5 seconds...
3ping 127.0.0.1 -n 6 > nul
4echo Done.

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:

bat
@echo off
ping 192.0.2.1 -n 1 -w 5000 > nul

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 -w variant 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:

bat
1@echo off
2echo Starting...
3powershell -NoProfile -Command "Start-Sleep -Seconds 5"
4echo Done.

For sub-second precision:

bat
powershell -NoProfile -Command "Start-Sleep -Milliseconds 2500"

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

bat
1@echo off
2powershell -NoProfile -Command ^
3    "if ((Get-Date).Hour -lt 6) { Start-Sleep -Seconds 300 } else { Start-Sleep -Seconds 5 }"
4echo Continuing...

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

MethodAvailabilityPrecisionUser-interruptibleOverhead
timeout /t 5 /nobreakWindows Vista+1 secondNo (with /nobreak)None
timeout /t 5Windows Vista+1 secondYes (any key cancels)None
ping 127.0.0.1 -n 6All WindowsApproximateNoMinimal
ping 192.0.2.1 -n 1 -w 5000All WindowsApproximateNoMinimal
powershell Start-SleepWindows 7+ (PS installed)MillisecondNo200-500ms startup

pause vs timeout: They Solve Different Problems

pause and timeout are frequently confused, but they serve entirely different purposes:

bat
1@echo off
2REM pause: waits for USER INPUT (indefinite)
3echo Press any key to deploy...
4pause > nul
5
6REM timeout: waits for a TIME DURATION (automatic)
7echo Deploying in 5 seconds...
8timeout /t 5 /nobreak > nul
9echo Deployed.

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

bat
1@echo off
2set RETRY=0
3set MAX_RETRY=5
4
5:retry_loop
6set /a RETRY+=1
7echo Attempt %RETRY% of %MAX_RETRY%...
8
9curl -s -o nul -w "%%{http_code}" http://localhost:8080/health | findstr "200" > nul
10if not errorlevel 1 (
11    echo Service is healthy.
12    goto :done
13)
14
15if %RETRY% GEQ %MAX_RETRY% (
16    echo Service did not become healthy after %MAX_RETRY% attempts.
17    exit /b 1
18)
19
20echo Waiting before retry...
21timeout /t %RETRY% /nobreak > nul
22goto :retry_loop
23
24:done
25echo Continuing with deployment.

This pattern increases the wait time on each retry (1s, 2s, 3s, etc.), giving the service progressively more time to start.

Countdown with progress

bat
1@echo off
2echo Shutting down in:
3for /L %%i in (5,-1,1) do (
4    echo   %%i...
5    timeout /t 1 /nobreak > nul
6)
7echo Shutdown initiated.

Output:

text
1Shutting down in:
2  5...
3  4...
4  3...
5  2...
6  1...
7Shutdown initiated.

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:

bat
1@echo off
2echo Waiting for build output...
3:wait_for_build
4if exist "build\output.jar" goto :build_done
5timeout /t 2 /nobreak > nul
6goto :wait_for_build
7
8:build_done
9echo Build complete. Deploying...

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 > nul is the standard, preferred way to sleep in modern batch files.
  • ping is a legacy workaround for systems without timeout. Use -n 6 for a 5-second delay (not -n 5).
  • PowerShell Start-Sleep provides millisecond precision but adds process startup overhead.
  • Use pause only 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 /nobreak in unattended scripts to prevent accidental early cancellation.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions