Sleep command in Windows (delay)

There is no straight forward sleep command in Windows. All Unix variants provides this command to provide delay in shell scripts. I circumvent this I am using ping command as given below which provides sleep functionality with approximate accuracy. Ping command is used to check the connectivity between to machines in a network.

The below command will create 2 seconds delay. Let’s see how it works.

ping -w 1000 -n 2 > NULL

ping : Ping the specified host machine

-w timeout: Timeout in milliseconds to wait for each reply (I have selected 1000 to make unit as 1 second)

-n count: Number of echo requests to send (No.of seconds to sleep) Dummy IP Address

> NULL: To eliminate the unwanted message

To adjust for better accuracy you can play with different values for -w argument. You can have this command in a batch file called sleep.bat and pass time in seconds as argument like this. Here is the sleep.bat

@echo off
ping -w 1000 -n %1 > NUL

Call this batchfile as “sleep 2” for seconds and so.

Note: Windows Resource kit provides this functionality, but it involves agreeing to some license and massive download – where sleep is part of a big tool kit. It is available here.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *