Batch file - restart program after every 20 minutes - batch-file

I want to create batch file which starts a program and after 20 minutes will close the program and start it again.
The only thing I know about a batch file is how to start a program:
#echo off
Start [adress of application]

This works:
#echo off //Turn off screen text messages
:loop //Set marker called loop, to return to
start "Wicked_Article_Creator" "C:\Wicked Article Creator\Wicked Article Creator.exe" //Start program, with title and program path
timeout /t 1200 >null //Wait 20 minutes
taskkill /f /im "Image Name" >nul //Image name, e.g. WickedArticleCreator.exe, can be found via Task Manager > Processes Tab > Image Name column (look for your program)
timeout /t 7 >null //Wait 7 seconds to give your prgram time to close fully - (optional)
goto loop //Return to loop marker

#echo off
:loop
start yourtarget.exe ...
timeout /t 1200 >null
taskkill /f /im yourtarget.exe >nul
goto loop
should do the job.

For anyone coming across this old question:
Instead of timeout.exe you can also ping to an address that definitely doesn't exist:
ping 999.199.991.91 -n 1 -w 60000 >NUL
You can change 60000 to whatever delay you want (in ms).
1 second = 1000
10 seconds = 10000
60 seconds = 60000
And so on..

#echo off
:loop
timeout /t 20 >null ( Wait for 20 seconds before kill program)
taskkill /F /IM terminal.exe
timeout /t 3600 >null ( wait for 1hr before returning to loop or recycle the process)
goto loop
I use another program called mt4bar to monitor my application and relaunch them anytime they crash or get closed

Related

How to make a .bat file check for a program and relaunch if it isn't open, also restarting the program if it's been running for x amount of time?

This can be two separate .bat files if needed, but I would prefer if it's possible on one.
I need to run a certain program, but it sometimes crashes.
I have this for a .bat so far.
It works as far as resetting the program every hour, but it does crash sometimes in between restarts.
I'd like to just have a check, so it launches if the program isn't found.
Is that possible?
#echo off
:loop
start "xx" "xxpath"
timeout /t 3600 >null
taskkill /f /im "xx" >null
timeout /t 4 >null
goto loop
Here is a sample batch that can check if any instance of chrome.exe is running or not
If not, we start it !
#echo off
Color 0A
Set WaitTimeSeconds=20
Set App_Path=C:\Program Files\Google\Chrome\Application\chrome.exe
for %%i in (%App_Path%) do set App_Name=%%~nxi
Title Checking if any "%App_Name%" instance is running ...
:Loop
Rem Clear the screen
cls
Rem Kill any application that have a status equal to not responding !
Taskkill /f /fi "status eq not responding">nul 2>&1
Rem Check if any application instance is running ; if not we start it !
TASKLIST | FINDSTR %App_Name% || START "" "%App_Path%"
Timeout /t %WaitTimeSeconds% /nobreak>nul
Goto Loop

Can I slow down the speed of a batch file?

I am trying to write an ASCII movie in batch, and I need the script to echo a new line at about 10-15 lines per second (hopefully that's not too confusing). By default, the script goes insanely fast, making the whole thing more difficult to see.
What I would like to do is be able to set the execution speed of the batch file, and NOT the computer itself.
Here's an example of what I'd like:
#echo off
REM Below command sets script execution speed in lines per second
setspeed=10lps
REM Ten lines of 03 begin
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
REM Ten lines of 03 end
REM Ten lines of 04 begin
echo The time is 13:20:04
echo The time is 13:20:04
.....
Thanks!
Update: I found out that if you download NirCmd (link is at the bottom of the page), it includes a "wait" command that works quite well. In my batch script, I did this:
#echo off
::This tells NirCmd to wait for 5 seconds, and then proceed to the next command.
nircmd wait 5000
echo This is one line
nircmd wait 5000
echo This is another line
nircmd wait 5000
echo Oh, hey! Another line!
nircmd wait 5000
echo Take a guess what this is... Another line!
...
I like using choice commands. For instance:
#echo off
echo this is one line
choice /c q /d q /t 1 >nul
echo this is the second line
You can change the number after /t to set the seconds to wait. You can also press Q to skip the wait at any time.
In newer Windows OSes, there is the TIMEOUT command as well:
TIMEOUT /T 5
waits 5 seconds and displays a countdown like Waiting <countingDown> seconds. Proceed with any key.
TIMEOUT /T 5 > NUL
does the same without displaying anything.
TIMEOUT /T 5 /NOBREAK > NUL
additionally removes the possibility to speed up by pressing a key.
Bad thing: Only complete seconds possible, no millis.
TIMEOUT /T x > NUL
(replace the X with the number of seconds you want)
This will do a hidden countdown because of the > NUL command at the end you won't see the countdown/pause/sleep/timer and then it will proceed with the next command. This is the best to use, as I found the Timeout command with the NUL at the end to be the best many of the other commands like Sleep and Pause do not work.
You can also download NODEJS to get more functions or switch to powershell.
TIMEOUT /T 5 > NUL
Above command will give 5 second timed countdown with NO display, pressing any key will not stop it unless you use CTRL+C.

End a command after a given time in a bat file

I have a bat file to execute several programs, and there is a possibility that one program stays in a loop. And I would like to kill the execution of that program after 1 min and execute the next one.
The programs I would like to execute gif_0.exe, gif_1.exe, ... receiving inputs from txt and writing the output to another txt.
gif_0.exe input1.txt output1_0.txt
timeout /t 20
gif_0.exe input2.txt output2_0.txt
timeout /t 20
gif_1.exe input3.txt output3_0.txt
timeout /t 20
gif_2.exe input4.txt output4_0.txt
timeout /t 20
gif_3.exe input5.txt output5_0.txt
My idea is similar to Geisterfurz007.
But my batch starts the exe in parallel and also another instance of the batch with the name of the started exe as an arg. The new instance checks the arg and jumps to the sub where it waits for the timeout and tries to kill the exe.
#echo off
setlocal EnableDelayedExpansion
Set Wait=60
If "%1" Neq "" Goto :KillTask
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
Start %~f0 gif_%%n.exe
)
Goto :Eof
:KillTask %1
timeout /t %Wait% /nobreak>nul
taskkill /F /IM %1
start "Title goes here" gif_0.exe input1.txt output1_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_0.exe
Should do the trick. Repeat after your needs.
Explanation:
starts your application with the given parameters
waits 60 seconds without the option to stop the timer before counting down and without any output.
Kills the task with the imagename of your application. If you may have several instances of this paralelly running this will kill all of them!
You can use filters to narrow down on one though; have a look at taskkill /?
I am not sure if there is a typo in your question but assuming your applications are gif_n.exe with the parameters inputn+1.exe outputn+1.exe
You can do the following:
#echo off
setlocal EnableDelayedExpansion
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_%%n.exe
)
This would go from 0 to n in single steps and would execute the same thing like above in a loop.
2 additions here:
The line setlocal EnableDelayedExpansion is needed to activate the possibility to use variables values after they were changed within a closed block of parenthesis like a for-loop or an if-condition.
set /a foo=%%n+1 will set the value of variable foo to the value of %%n+1. The switch /a is needed to perform an actual calculation and not the appending of a string.
To use the variable in our for-loop we have to use the DelayedExpasion (see above):
Simply change the % you would usually use to ! and you are done.
To make sure, everything works correctly you might want to place an echo in front of your three main lines.
Feel free to ask questions if something is unclear :)

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.
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
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
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).

Batch-file to run executable on time interval

I'm trying to create a .bat file to execute a flash video on a 10 s time interval in an infinite loop. So far I have...
ECHO OFF
:TOP
START /d "path" program.exe
SLEEP 10
TASKKILL /F /IM program.exe
GOTO TOP
It needs to restart the video after 10 seconds, but it just turns it on and that's it. Help!
Try this instead:
#echo off
:TOP
PathDir\program.exe
ping 127.0.0.1 -n 11
taskkill /im program.exe /f
goto :TOP
Haven't changed much but I haven't used SLEEP so not sure if that's causing the issue.
#echo off
#color 0a
:top
echo sleeping... sleeping...
sleep 5
goto top
echo waking... yargh!!! how dare yee wake thy neighbroseph?!
Also be sure to have this .bat file residing in the same directory as Sleep.exe which can be found: http://www.computerhope.com/dutil.htm
Tested and working exactly how I'd hoped.

Resources