Execute a command every n seconds?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Execute a command every n seconds?

#1 Post by pstein » 13 Apr 2017 23:50

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

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: Execute a command every n seconds?

#2 Post by SIMMS7400 » 14 Apr 2017 01:39

Code: Select all

:LOOP

<execute command here>

PING -n 5 127.0.0.1>nul

GOTO LOOP

dubcusb
Posts: 3
Joined: 13 Oct 2016 16:21

Re: Execute a command every n seconds?

#3 Post by dubcusb » 14 Apr 2017 12:46

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.

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: Execute a command every n seconds?

#4 Post by SIMMS7400 » 15 Apr 2017 06:12

Certain utilities don't like TIMEOUT, such as PsExec.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Execute a command every n seconds?

#5 Post by Squashman » 15 Apr 2017 22:00

SIMMS7400 wrote:Certain utilities don't like TIMEOUT, such as PsExec.

Would love to see an example.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Execute a command every n seconds?

#6 Post by ShadowThief » 15 Apr 2017 22:10

I could have sworn there was some version of Windows that didn't come with timeout...

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Execute a command every n seconds?

#7 Post by penpen » 16 Apr 2017 02:17

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

Post Reply