Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How to pause execution for a while in a Windows batch file between a command and the next one?

share|improve this question
    
Is this intended to become a canonical? If so then some explanatory text might be appropriate. –  John Gardeniers Sep 26 '12 at 23:06
1  
"Explanatory text" as in? –  Massimo Sep 27 '12 at 7:00
2  
Most, if not all, canonical questions I've seen include text describing the intent of that question. –  John Gardeniers Sep 27 '12 at 8:52
    
Or some text explaining the situation... It's mostly to help with SEO, but also helps to frame potential Answers. –  Chris S Sep 28 '12 at 1:12
    
I actually thought about it, but this just seemed something too silly to be explicitly included in the Big List of Canonical Questions... meta.serverfault.com/questions/1986/… –  Massimo Sep 28 '12 at 6:35
add comment

2 Answers

up vote 46 down vote accepted

The correct way to sleep in a batch file is to use the timeout command, introduced in Windows 2000.

To wait 30 seconds:

timeout /t 30

The timeout would get interrupted if the user hits any key; however, the command also accepts the optional switch /nobreak, which effectively ignores anything the user may press, except an explicit CTRL-C:

timeout /t 30 /nobreak

Additionally, if you don't want the command to print its countdown on the screen, you can redirect its output to NUL:

timeout /t 30 /nobreak > NUL
share|improve this answer
7  
Wow. How did I not know about this. +1 –  MDMarra Sep 26 '12 at 20:13
5  
Since Windows 2000 too! I've definitely downloaded sleep.exe from the Resource Kit at least a couple of times in the last ten years, completely oblivious to the existence of this command. –  Chris McKeown Sep 26 '12 at 20:40
1  
+1 from another sleep user, yet I'm sure I have used timeout before but simply forgot about it. –  John Gardeniers Sep 26 '12 at 21:50
    
@ChrisMcKeown Technet says Windows 2000, but maybe it was a resource kit tool on that system (I don't have a 2000 box at hand to check it); however, I know for sure it has been included natively at least since XP/2003. –  Massimo Sep 28 '12 at 6:36
    
The timeout command does not work with Windows XP even with the 2003 Resource kit installed and the sleep command does not work in Windows 7 so the ping command can still be of good use if you are using the batch file on both Win7 and WinXP. –  user178627 Jun 20 '13 at 22:55
add comment

You can also insert a ping to localhost. This will take 4 seconds to complete (by default). It is considered a kludge by some, but works quite well all the same.

The command:
ping 127.0.0.1

share|improve this answer
4  
Ehm... the whole purpose of this Q/A was exactly to correct two other questions which have this as their accepted answer, but can no longer be corrected because their OP is no longer around. –  Massimo Sep 26 '12 at 20:13
5  
While it will work it's the equivalent of driving a screw with a hammer. A wrong tool may work, but it's still wrong. –  voretaq7 Sep 26 '12 at 20:16
1  
@MarkAllen In other words, Windows NT4, 95, 98, ME and older? –  Chris McKeown Sep 26 '12 at 23:44
1  
@MarkAllen This is indeed true, but it's also the reason people are still using this kludge even when better and more suited tools have been available for a while (and let's not even get into talk about who uses a choice with a timeout). A little bit of reasearch into using proper tools for the job should be the duty of any computing professional (or any professional at all). –  Massimo Sep 27 '12 at 7:03
3  
@MarkAllen - technically you then have to worry about whether or not ping comes with your OS. If you're worried enough about '95 '98 or ME then you should be as equally worried about 6.22 which doesn't have ping either –  Mark Henderson Sep 27 '12 at 20:36
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.