How to sleep for five seconds in a batch file/cmd
One hack is to (mis)use the ping command:
ping 127.0.0.1 -n 6 > nul
Explanation:
ping
is a system utility that sends ping requests.ping
is available on all versions of Windows.127.0.0.1
is the IP address of localhost. This IP address is guaranteed to always resolve, be reachable, and immediately respond to pings.-n 6
specifies that there are to be 6 pings. There is a 1s delay between each ping, so for a 5s delay you need to send 6 pings.> nul
suppress the output ofping
, by redirecting it tonul
.
Sleeping in a batch file
UPDATE
The timeout
command, available from Windows Vista and onwards should be the command used, as described in another answer to this question. What follows here is an old answer.
Old answer
If you have Python installed, or don't mind installing it (it has other uses too :), just create the following sleep.py script and add it somewhere in your PATH:
import time, sys
time.sleep(float(sys.argv[1]))
It will allow sub-second pauses (for example, 1.5 sec, 0.1, etc.), should you have such a need. If you want to call it as sleep
rather than sleep.py
, then you can add the .PY
extension to your PATHEXT environment variable. On Windows XP, you can edit it in:
My Computer → Properties (menu) → Advanced (tab) → Environment Variables (button) → System variables (frame)
sleep function in a batch script
You can also use timeout
.
Here is an example:
@echo off
echo Hi
timeout /t 1 /nobreak > nul
/t
is not mandatory1 is the amount of second(s) to wait
/nobreak
ensures the user can't skip the wait> nul
redirects output to nothing, so you don't see anything
Batch Script - Wait 5 seconds before exec program
There are (at least) the following options, as others already stated:
To use the
timeout
command:rem // Allow a key-press to abort the wait; `/T` can be omitted:
timeout /T 5
timeout 5
rem // Do not allow a key-press to abort the wait:
timeout /T 5 /NOBREAK
rem // Suppress the message `Waiting for ? seconds, press a key to continue ...`:
timeout /T 5 /NOBREAK > nulCaveats:
timeout
actually counts second multiples, therefore the wait time is actually something from 4 to 5 seconds. This is particularly annoying and disturbing for short wait times.- You cannot use
timeout
in a block of code whose input is redirected, because it aborts immediately, throwing the error messageERROR: Input redirection is not supported, exiting the process immediately.
. Hencetimeout /T 5 < nul
fails.
To use the
ping
command:rem /* The IP address 127.0.0.1 (home) always exists;
rem the standard ping interval is 1 second; so you need to do
rem one more ping attempt than you want intervals to elapse: */
ping 127.0.0.1 -n 6 > nulThis is the only reliable way to guarantee the minimum wait time. Redirection is no problem.
Sleep command in batch file?
I think the information here: http://malektips.com/xp_dos_0002.html would explain it better than I.
There's still the case of error handling though (what if the remote machine isn't up?). cmd.exe is quite useless for doing any remote activities for the most part, using powershell would enable so much more.
EDIT::
In fact, you can execute a program stored locally with psexec (it gets copied across and executed locally server-side) - would using that be a more viable alternative?
Without knowing what commands you're intending to run it's hard to take it much further.
EDIT(2)::
If it's just the one command you're running, simply store it in a dedicated file, like 'remote_dir_listing.cmd', and then use psexec with:
psexec \\server -u <user> -p <pass> -c -f remote_dir_listing.cmd
This will force a copy of the local file to the remote side each time you execute it (in case you want to expand it). In this way, you bypass the need for a pause at all - only when psexec has got the pipes open will it run, and once it completes, it closes itself silently.
Is it possible to pause a Batch Script with no message?
You can redirect the timeout output to the nul device:
timeout 10 > nul
Windows batch: sleep
You can try
ping -n XXX 127.0.0.1 >nul
where XXX is the number of seconds to wait, plus one.
How to wait in a batch script?
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.
Related Topics
Obtain Active Window Using Python
How to Append One String to Another in Python
Elegant Python Function to Convert Camelcase to Snake_Case
How to Download a File on a Click Event Using Selenium
Subprocess.Call Using String VS Using List
Converting Xml to JSON Using Python
Using Python Iterparse for Large Xml Files
Thread Starts Running Before Calling Thread.Start
How to Read the Rgb Value of a Given Pixel in Python
Read Excel Cell Value and Not the Formula Computing It -Openpyxl
How to Run Python Code from Sublime Text 2
Traverse a List in Reverse Order in Python
How to Get First Element in a List of Tuples
How to Make Program Go Back to the Top of the Code Instead of Closing