9

I am creating a batch file to run a program on my desktop xyz.exe for 4 hours, then close it for 1 hour and repeat the process. Here is my script.

:a
START C:\Users\Mukul\Desktop\xyz.exe
SLEEP 14400
taskkill /F /IM xyz.exe
SLEEP 3600
goto :a

According to here, the script should wait. It also says:

SLEEP 10

will delay execution of the next command by 10 seconds. so SLEEP 14400 should delay the execution by 4 hours.

Current results: Next command gets executed as soon as the first command completed.

Desired results: Next command should wait for 4 hours before executing the last command.

Mukul Chauhan
  • 120
  • 1
  • 1
  • 10
  • You can also do this: `powershell sleep 14400` – man May 30 '17 at 13:53
  • 2
    Possible duplicate of [Windows batch: sleep](https://stackoverflow.com/questions/4317020/windows-batch-sleep) – phuclv Jun 20 '18 at 04:38
  • for windows 10 pro as of sept 21 2020, sleep is not recognized but timeout is. the parameter of timeout is in seconds. e.g Timeout /t 10
    For waiting indefinitely until a key press: timeout /t -1.
    for details see timeout /? in the command prompt
    – gg89 Sep 21 '20 at 17:15

3 Answers3

15

You can use bash's timeout command.

For example:

@echo off
echo Hi
timeout /t 1 /nobreak > nul
  • /t is not mandatory

  • 1 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

LeoDog896
  • 3,472
  • 1
  • 15
  • 40
5

SLEEP command may not be supported by your Windows version. Try this:

:a
START C:\Users\Mukul\Desktop\xyz.exe
TIMEOUT 14400
taskkill /F /IM xyz.exe
TIMEOUT 3600
goto :a
sambul35
  • 1,058
  • 14
  • 22
  • since when it doesn't support ? i use windows 10. thanks for answer – Mukul Chauhan Aug 13 '16 at 19:05
  • not sure when but probably since they start allowing windows key+sleep for "power and sleep setting" that can control time before windows goes to sleep – gg89 Sep 21 '20 at 21:18
2

First off: Bash and Batch are very different languages.
Now, the answer.
I prefer the ping command over the sleep command, for it's easy switching between seconds and milliseconds:

ping -n 11 127.0.0.1>nul

That command pauses for 10 seconds then resumes.
Or:

ping 1.1.1.1 -n 1 -w 10001 >nul

Which also pauses for 10 seconds, but in milliseconds.

Either can be adapted into:

:a
start C:\Users\Mukul\Desktop\xyz.exe
ping -n 14401 127.0.0.1>nul
taskkill /F /IM xyz.exe
ping -n 3601 127.0.0.1>nul
goto a

DISCLAIMER: I have NOT tried the final piece of code (because I don't have 4 extra hours to do something).

ender_scythe
  • 470
  • 7
  • 16
  • 4
    1.1.1.1 will actually go somewhere >> https://1.1.1.1/ (https://1.0.0.1/) – player0 May 12 '18 at 22:55
  • 3
    No, no and no. Even if this is the Microsoft Windows world, and not everything there has sense even if it sometime works, this is still information technology and pinging someone to wait some seconds can't be the answer. Moreover, why pinging a private host (`1.1.1.1` is Cloudflare) and not localhost? Downvote. – Valerio Bozz Apr 23 '20 at 10:01