Batch File
CMD
Programming
Sleep Function
Coding Tips

How to sleep for five seconds in a batch file/cmd

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Windows batch files do not have a built-in sleep keyword like some Unix shells, but delaying execution is still easy. In modern Windows environments the cleanest answer is timeout /t 5, while older compatibility scenarios sometimes still rely on ping or PowerShell.

Use timeout in Modern Batch Files

For most current systems, timeout is the right answer.

bat
1@echo off
2echo Waiting for 5 seconds...
3timeout /t 5 /nobreak > nul
4echo Done.

/t 5 means five seconds. /nobreak prevents the delay from being skipped by a key press. Redirecting to nul suppresses the default countdown text when you want cleaner output.

If you actually want the user to see the countdown, remove the redirection.

Use ping for Older Compatibility

Before timeout became common, batch files often used ping as a rough delay mechanism.

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

Why 6 instead of 5? Because ping sends one request immediately, then waits between the remaining sends. It is a workaround, not a true sleep function, so the timing is approximate.

Another version uses the timeout per ping request:

bat
ping 192.0.2.1 -n 1 -w 5000 > nul

That waits up to about five seconds for a single request to time out.

Use PowerShell When You Need Precision or Richer Scripting

If PowerShell is available and calling it is acceptable in your environment, you can use its direct sleep command.

bat
@echo off
powershell -NoProfile -Command "Start-Sleep -Seconds 5"

This is clear and readable, though it starts another process and is usually heavier than timeout for simple batch scripts.

Know the Difference Between Blocking and Waiting for Input

Sometimes what looks like a sleep requirement is really a pause for user confirmation. In that case, pause is the right command, not timeout.

bat
@echo off
echo Press any key to continue...
pause > nul

Use pause for user interaction. Use timeout or another sleep mechanism for automatic delays.

Build Delays into Real Script Flow Carefully

A batch delay should have a reason. Common cases include waiting for a service to start, spacing retries, or giving a child process time to create a file. Even then, a fixed delay is often weaker than actively checking the condition you care about.

For example, instead of always sleeping for five seconds, poll until the expected file appears.

bat
1@echo off
2:wait_for_file
3if exist output.txt goto done
4timeout /t 1 /nobreak > nul
5goto wait_for_file
6:done
7echo File is ready.

That pattern is more reliable than guessing how long the operation will take.

Choose the Simplest Tool That Matches the Environment

If the script targets standard modern Windows machines, use timeout. If you are stuck with older shells, ping remains a fallback. If your script is already mixing PowerShell and batch, Start-Sleep may be the cleanest readable option.

Common Pitfalls

  • Using pause when the goal is an automatic time delay rather than user interaction.
  • Forgetting /nobreak and allowing a key press to skip the wait unexpectedly.
  • Treating the ping workaround as exact timing instead of approximate delay.
  • Adding fixed sleeps when polling for the real condition would be more reliable.
  • Calling PowerShell for every tiny delay in a script where timeout is already available.

Summary

  • 'timeout /t 5 /nobreak is the usual best answer for batch scripts.'
  • 'ping works as an older compatibility workaround.'
  • PowerShell offers Start-Sleep when that environment is already part of the script.
  • Use pause only for user input, not for timed waiting.
  • Prefer checking for the real condition over adding arbitrary fixed delays.

Course illustration
Course illustration

All Rights Reserved.