10

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.

3
  • You can also do this: powershell sleep 14400
    – man
    Commented May 30, 2017 at 13:53
  • 2
    Possible duplicate of Windows batch: sleep
    – phuclv
    Commented Jun 20, 2018 at 4: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 <BR> For waiting indefinitely until a key press: timeout /t -1. <br> for details see timeout /? in the command prompt
    – gg89
    Commented Sep 21, 2020 at 17:15

3 Answers 3

16

You can use batch'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

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
2
  • since when it doesn't support ? i use windows 10. thanks for answer Commented Aug 13, 2016 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
    Commented Sep 21, 2020 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).

2
  • 4
    1.1.1.1 will actually go somewhere >> 1.1.1.1 (1.0.0.1)
    – player0
    Commented May 12, 2018 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. Commented Apr 23, 2020 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.