189

I am trying to write a batch script and trying to wait 10 seconds between 2 function calls. The command:

sleep 10

Does not make the batch file wait for 10 seconds.

I am running Windows XP.

Note: This is not a complete duplicate of Sleeping in a batch file as the other question is about also about python, while this is about windows batch files.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Thomaschaaf
  • 17,847
  • 32
  • 94
  • 128
  • Related post - [Windows batch: sleep](https://stackoverflow.com/q/4317020/465053) – RBT May 07 '21 at 07:37
  • Also see [timeout](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/timeout) – djvg Jun 29 '22 at 07:04

6 Answers6

286

You can ping an address that doesn't exist and specify the desired timeout:

ping 192.0.2.2 -n 1 -w 10000 > nul

And since the address does not exist, it'll wait 10,000 ms (10 seconds) and return.

  • The -w 10000 part specifies the desired timeout in milliseconds.
  • The -n 1 part tells ping that it should only try once (normally it'd try 4 times).
  • The > nul part is appended so the ping command doesn't output anything to screen.

You can easily make a sleep command yourself by creating a sleep.bat somewhere in your PATH and using the above technique:

rem SLEEP.BAT - sleeps by the supplied number of seconds

@ping 192.0.2.2 -n 1 -w %1000 > nul

NOTE (September 2020): The 192.0.2.x address is reserved as per RFC 3330 so it definitely will not exist in the real world. Quoting from the spec:

192.0.2.0/24 - This block is assigned as "TEST-NET" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. Addresses within this block should not appear on the public Internet.

reduckted
  • 2,358
  • 3
  • 28
  • 37
chakrit
  • 61,017
  • 25
  • 133
  • 162
  • 2
    ping 127.0.0.1 -n 5 -w 1000 > nul hat to do it like this since the other would finish right away. – Thomaschaaf Apr 09 '09 at 18:58
  • @Thomaschaaf interesting... pinging a local machine address should've caused ping to return *right away* since it's pinging the same machine (i.e. time<1ms) so the timeout trick shouldn't have worked in that case! – chakrit Apr 09 '09 at 19:04
  • 16
    I added the actual function its called timeout.. http://www.ss64.com/nt/timeout.html at least I know that others didn't know either :) – Thomaschaaf Apr 09 '09 at 19:42
  • Brilliant! Although I would have preferred Windows to supply a default command! – divesh premdeep Jun 26 '11 at 19:54
  • How about `choice /c:Z /d:Z /t %seconds% > nul`? – Derek 朕會功夫 Mar 21 '12 at 01:35
  • winxp pro: 'choice' is not recognized as an operable program or batch file. – peter Oct 15 '12 at 11:27
  • 3
    doesn't work, `ping` responds with: `PING: transmit failed. General failure.` probably because I'm in a 10.x.x.x range. – huysentruitw May 28 '13 at 07:13
  • 2
    I was happily using `ping 0.0.0.0 -n 1 -w 10000 > nul` until Windows 10 which will immediately return with this particular IP, without any waiting. `@ping 1.0.0.0 -n 1 -w 3000 > nul` works. – Vlastimil Ovčáčík Aug 21 '15 at 13:36
151

You'd better ping 127.0.0.1. Windows ping pauses for one second between pings so you if you want to sleep for 10 seconds, use

ping -n 11 127.0.0.1 > nul

This way you don't need to worry about unexpected early returns (say, there's no default route and the 123.45.67.89 is instantly known to be unreachable.)

Gleb
  • 2,404
  • 1
  • 13
  • 7
  • 27
    I like this solution more than the the one marked as the answer because this is cleaner as we don't have to make sure that our script has a non-existent IP address. Therefore, we can write the script once and use it on any Windows system in any network without editing the script again. – Susam Pal Jul 31 '12 at 11:34
  • 4
    Since [RFC 3330](http://smakd.potaroo.net/ietf/idref/rfc3330/) became obsoleted I'm using this solution. – Reyno Sep 19 '13 at 07:58
  • 4
    Or perhaps use localhost to avoid problems on IPv6-only machines. – Oliver Bock Oct 24 '14 at 05:14
139

I actually found the right command to use.. its called timeout: http://www.ss64.com/nt/timeout.html

Thomaschaaf
  • 17,847
  • 32
  • 94
  • 128
49

I used this

:top
cls
type G:\empty.txt
type I:\empty.txt
timeout /T 500
goto top
7

What about:

@echo off
set wait=%1
echo waiting %wait% s
echo wscript.sleep %wait%000 > wait.vbs
wscript.exe wait.vbs
del wait.vbs
moinudin
  • 134,091
  • 45
  • 190
  • 216
L3X0R
  • 79
  • 1
  • 1
  • 2
    This is the only solution that seems to be accurate even for delays that are `<1000ms`. – Lynn Jul 20 '15 at 17:19
6

Well, does sleep even exist on your Windows XP box? According to this post: http://malektips.com/xp_dos_0002.html sleep isn't available on Windows XP, and you have to download the Windows 2003 Resource Kit in order to get it.

Chakrit's answer gives you another way to pause, too.

Try running sleep 10 from a command prompt.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
easel
  • 3,982
  • 26
  • 28