Page 1 of 1

Execute a command every n seconds?

Posted: 13 Apr 2017 23:50
by pstein
How can I execute (from a DOS batch script) a command every n (e.g. 5) seconds?

The command output should be shown at the command prompt "as usual".

This loop should be stoppable only by hitting Ctrl+C (or by closing the cmd.exe window by clicking the "x")

Peter

Re: Execute a command every n seconds?

Posted: 14 Apr 2017 01:39
by SIMMS7400

Code: Select all

:LOOP

<execute command here>

PING -n 5 127.0.0.1>nul

GOTO LOOP

Re: Execute a command every n seconds?

Posted: 14 Apr 2017 12:46
by dubcusb
SIMMS7400 wrote:

Code: Select all

:LOOP

<execute command here>

PING -n 5 127.0.0.1>nul

GOTO LOOP


Why use "ping" instead of "timeout"? timeout is more precise and easier to remember.

Code: Select all

:loop

<command>

timeout /nobreak /t <time in seconds> > NUL

goto loop


Add "> NUL" at the end to hide the timeout output.

Re: Execute a command every n seconds?

Posted: 15 Apr 2017 06:12
by SIMMS7400
Certain utilities don't like TIMEOUT, such as PsExec.

Re: Execute a command every n seconds?

Posted: 15 Apr 2017 22:00
by Squashman
SIMMS7400 wrote:Certain utilities don't like TIMEOUT, such as PsExec.

Would love to see an example.

Re: Execute a command every n seconds?

Posted: 15 Apr 2017 22:10
by ShadowThief
I could have sworn there was some version of Windows that didn't come with timeout...

Re: Execute a command every n seconds?

Posted: 16 Apr 2017 02:17
by penpen
Yes, Windows XP has no "timeout.exe".
But for this task you could use "sleep.exe" which is contained in the Windows Server 2003 Resource Kit Tools:
If you don't need any other tool, then you could open "rktools.exe" using for example "7zip", then
"Open Inside" the (packed) file "rktools.msi", mark "sleep.exe" and extract just that file only.


penpen