Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sleep function in a batch script

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.

like image 322
Mukul Chauhan Avatar asked Aug 13 '16 18:08

Mukul Chauhan


People also ask

How do I create a timeout in a batch file?

Delay execution for a few seconds or minutes, for use within a batch file. Syntax TIMEOUT delay [/nobreak] Key delay Delay in seconds (between -1 and 100000) to wait before continuing. The value -1 causes the computer to wait indefinitely for a keystroke (like the PAUSE command) /nobreak Ignore user key strokes.

What is sleep command in CMD?

The “sleep” command is used to add delay in some processes but unfortunately sleep or delay commands do not support windows operating systems. In the Windows operating system, we can utilize the “timeout” command that can delay the execution of commands for some seconds or for a specified time.

What is %% A in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Is there a sleep command in Windows?

To set your PC so it goes to sleep when you close the lid or press the power button: Open power options—select Start , then select Settings > System > Power & sleep > Additional power settings.


2 Answers

You can also use timeout.

Here is an 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

like image 75
LeoDog896 Avatar answered Sep 28 '22 02:09

LeoDog896


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
like image 23
sambul35 Avatar answered Sep 28 '22 00:09

sambul35