Windows: Need to force close a process every few minutes so that batch script can start it up again - windows

I have a batch script that starts up a process whenever it detects that the process was closed. I need to force close the process somehow on a set interval loop to see if the batch script is working as it should. What would be the best way to do this? I tried using another batch script to close the process, but didn't get the result as expected since both batch scripts open in the same command window.

In a second script, loop with goto and use a ping for pause.
:loop
:: 301 = 5 minutes. The first ping response is instant.
ping -n 301 0.0.0.0 >NUL
taskkill /im "program.exe" /f
goto loop

Related

How to end only a single task using taskkill?

I have a .bat file that end another .bat file using taskkill but it ends up killing itself and the other .bat file.
So how do I make sure it kills only the oldest .bat file?
Because the 2nd bat file runs after the first
I can't use PID because the PID changes with each run!
taskkill /im cmd.exe
This kills all the cmd procceses including itself! I need it to kill only 1 of the processes. (that's the older 1)
I have ran into this problem before and the way I solved it is:
Have the first script (the file you want to close) looking to see if a temporary file has been made, and if it has, delete the file the close.
:loop
if EXIST %TEMP%\close.tmp (
del %TEMP%\close.tmp
exit
)
other code
goto :loop
And in the second script (the one closing the first script) just make it create the temporary file.
echo do you want to close SCRIPT1?
choice
if %ERRORLEVEL%==1 echo.>%TEMP%\close.tmp
And if you want to, you can make it so the first script runs more commands before it closes.

Close and restart a process after a certain time period in batch

I'm trying to make a batch file that opens a certain exe and then, after (for example) 5 minutes, closes it then reopens it again. I have tried this:
#echo off
:loop
cd /d %~dp0
certain exe
timeout /t "time" (by minutes)
taskkill /f /im "certain exe"
goto loop
but it wouldn't close the exe nor open it, what can I do?
The problem is that cmd will wait for the current command to end before executing the next command, so timeout (and taskill) will not be executed until example.exe closes by itself.
To ensure that cmd doesn't wait example.exe, you'll need to use start /B, as in this example:
#echo off
:loop
start /B "example.exe"
ping -n seconds_of_delay+1 127.0.0.1>nul
taskkill /f /im "example.exe"
goto :loop
Replace seconds_of_delay+1 with the number of seconds you want the time period to last plus one.
/B is needed because, without it, start would execute example.exe in a new window (if you want that, simply use start).
I used ping as the way to create a time delay because it has been found to consume less processor time than sleep or timeout (you can find more details here).

Need a batch file to start, delay a close, and restart another batch file.

I have searched and searched and this is the closest code I have found:
#echo off
:loop
C:\CryptoCurrency\nexus_cpuminer\start.bat
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
A few things: notice the start.bat. The .exe I need to launch has to start via the .bat file because the .bat file contains information the .exe needs.
Secondly, the .exe launches a CMD prompt window which shows me what's going on.
(keep this in mind because this is not your normal .exe, I WANT that CMD prompt window to close when it's KILLED)
I am aware I have it set for 30 seconds. I'm just testing right now. I'd like to set it for 4 hours before the kill command is called. Also, I'd like to set a "delay" of 30 seconds before the whole process starts over. I am running Windows 7 x 64.
You must change the name of the second Batch file to other name (i.e. starter.bat) and execute it via the start internal command in order to execute it in parallel:
#echo off
:loop
start "" cmd /C "C:\CryptoCurrency\nexus_cpuminer\starter.bat"
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
The last line in starter.bat file must be the execution of nexus_cpuminer.exe, so when it is killed via taskkill, the .bat file ends immediately.
Another simpler approach is to directly execute nexus_cpuminer.exe in this Batch file, via start "" cmd /C nexus_cpuminer.exe command, so this process be opened in its own cmd.exe window.
If you CALL start.bat, it will return to your 'calling' script.
If you give start.bat a TITLE, you can /FIlter your TASKKILL command to EQ that WINDOWTITLE

Need to run an .exe, then kill it after ~10 seconds and move to next command in batch file

I am attempting to create a batch file that runs an .exe, but kills said .exe after about 10 seconds (before it completes), then moves on to the next command in the file. Any help would be greatly appreciated.
You can use this:
start program.exe
ping 127.0.0.1 -n 10
taskkill /im program.exe /f
rem continue here
echo Another command!

How to continuously execute an application with CMD?

I need to execute a synchronizing application continuously with a batch file. What I want to do is something like:
while true
execute application
sleep 1 minute
end while
Can this be done with CMD?
Thanks in advance.
Using pure batch you could do this:
:LOOP
yourprogram.exe
ping 127.0.0.1 -n 61 >NUL
goto :LOOP
This means you don't have to install any other programs or create more scripts. It might not be pretty but it works! I have pinged 61 times as pinging the loopback seems to create a delayed second.
Download unixutils for win32. In here, you will find sleep.exe. Put it somewhere in your path. (frex, in c:\windows)
Then, you can build a batch file similar to this
echo off
:here
sleep 60s
echo Exec app
rem your app goes here
goto here

Resources