Sleeping in a batch file

2019-01-01 13:10发布

When writing a batch file to automate something on a Windows box, I've needed to pause its execution for several seconds (usually in a test/wait loop, waiting for a process to start). At the time, the best solution I could find uses ping (I kid you not) to achieve the desired effect. I've found a better write-up of it here, which describes a callable "wait.bat", implemented as follows:

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

You can then include calls to wait.bat in your own batch file, passing in the number of seconds to sleep.

Apparently the Windows 2003 Resource Kit provides a Unix-like sleep command (at last!). In the meantime, for those of us still using Windows XP, Windows 2000 or (sadly) Windows NT, is there a better way?

I modified the sleep.py script in the accepted answer, so that it defaults to one second if no arguments are passed on the command line:

import time, sys

time.sleep(float(sys.argv[1]) if len(sys.argv) > 1 else 1)

30条回答
姐姐魅力值爆表
2楼-- · 2019-01-01 13:16

Using the ping method as outlined is how I do it when I can't (or don't want to) add more executables or install any other software.

You should be pinging something that isn't there, and using the -w flag so that it fails after that amount of time, not pinging something that is there (like localhost) -n times. This allows you to handle time less than a second, and I think it's slightly more accurate.

e.g.

(test that 1.1.1.1 isn't taken)

ECHO Waiting 15 seconds

PING 1.1.1.1 -n 1 -w 15000 > NUL
  or
PING -n 15 -w 1000 127.1 >NUL
查看更多
人间绝色
3楼-- · 2019-01-01 13:17

Depending on your compatibility needs, either use ping:

ping -n <numberofseconds+1> localhost >nul 2>&1

e.g. to wait 5 seconds, use

ping -n 6 localhost >nul 2>&1

or on Windows 7 or later use timeout:

timeout 6 >nul
查看更多
ら面具成の殇う
4楼-- · 2019-01-01 13:19

The timeout command is available from Windows Vista onwards:

c:\> timeout /?

TIMEOUT [/T] timeout [/NOBREAK]

Description:
    This utility accepts a timeout parameter to wait for the specified
    time period (in seconds) or until any key is pressed. It also
    accepts a parameter to ignore the key press.

Parameter List:
    /T        timeout       Specifies the number of seconds to wait.
                            Valid range is -1 to 99999 seconds.

    /NOBREAK                Ignore key presses and wait specified time.

    /?                      Displays this help message.

NOTE: A timeout value of -1 means to wait indefinitely for a key press.

Examples:
    TIMEOUT /?
    TIMEOUT /T 10
    TIMEOUT /T 300 /NOBREAK
    TIMEOUT /T -1

Note: It does not work with input redirection - trivial example:

C:\>echo 1 | timeout /t 1 /nobreak
ERROR: Input redirection is not supported, exiting the process immediately.
查看更多
几人难应
5楼-- · 2019-01-01 13:20

Even more lightweight than the Python solution is a Perl one-liner.

To sleep for seven seconds put this in the BAT script:

perl -e "sleep 7"

This solution only provides a resolution of one second.

If you need higher resolution then use the Time::HiRes module from CPAN. It provides usleep() which sleeps in microseconds and nanosleep() which sleeps in nanoseconds (both functions takes only integer arguments). See the Stack Overflow question How do I sleep for a millisecond in Perl? for further details.

I have used ActivePerl for many years. It is very easy to install.

查看更多
宁负流年不负卿
6楼-- · 2019-01-01 13:21

You could use the Windows cscript WSH layer and this wait.js JavaScript file:

if (WScript.Arguments.Count() == 1)
    WScript.Sleep(WScript.Arguments(0)*1000);
else
    WScript.Echo("Usage: cscript wait.js seconds");
查看更多
浮光初槿花落
7楼-- · 2019-01-01 13:21

You can also use a .vbs file to do specific timeouts:

The code below creates the .vbs file. Put this near the top of you rbatch code:

echo WScript.sleep WScript.Arguments(0) >"%cd%\sleeper.vbs"

The code below then opens the .vbs and specifies how long to wait for:

start /WAIT "" "%cd%\sleeper.vbs" "1000"

In the above code, the "1000" is the value of time delay to be sent to the .vbs file in milliseconds, for example, 1000 ms = 1 s. You can alter this part to be however long you want.

The code below deletes the .vbs file after you are done with it. Put this at the end of your batch file:

del /f /q "%cd%\sleeper.vbs"

And here is the code all together so it's easy to copy:

echo WScript.sleep WScript.Arguments(0) >"%cd%\sleeper.vbs"
start /WAIT "" "%cd%\sleeper.vbs" "1000"
del /f /q "%cd%\sleeper.vbs"
查看更多
登录 后发表回答