Batch script to kill a process if not responding in a for loop - for-loop

I am trying to install some windows standalone update files and to do this I need to use wusu.exe. Every now and again wusu.exe will hang. I have created a batch file called prereqs.bat and in this file I have calls to the wusu.exe. I need the code to kill wusu.exe if it hangs, and retry it again.
This is my code as it stands now:
:PreReqs32
taskkill /im prereqs32.bat /f
taskkill /im wusa.exe /f
when it loops back, it kills both the batch file and wusa.exe
start cmd /k c:\windows\temp\prereqs.bat
An outside process so that I can kill wusu.exe if things go awry.
timeout /t 240 /NOBREAK
this timeout is to wait until the install is complete which is sometimes not enough.
taskkill /im "[wusa.exe]" /fi "STATUS eq NOT RESPONDING"
if "%ERRORLEVEL%"=="1" goto PreReqs32
Is there a way for some sort of FOR loop, with logic to exit if the status is not "not responding"?
Also, as a bonus, is there a way to forgo the timeout and just "wait" for prereqs.bat to be complete before moving on, assuming that wusu.exe has not hung?

Related

.bat to check if a window or process is open or running and if it is do this if it is not do something else

I need a little help here.
I need to create a .bat file to check if a process is running every 5 seconds. If the process is running then it simply resets and checks again in 5 seconds. If the process is not running, then it closes all other .bat files and restarts everything.
This is for a game server. So right now the whole process includes a few .bat files of which do different things. The initiate.bat starts the process by starting startserver.bat. startserver.bat then starts the game server, it's window opens and the server starts, it the starts the .bat file schedule.bat which sets a timer and is meant to restart the server once the timer has run out and then startserver.bat starts up anticrash.bat of which the code for it is below.
I have a feeling the code is not finding the server window or the process. I've double checked the process file name and have made sure that is correct. The title in the window for the server is simply the path to the .exe, but no matter what I've tried I can't get it to function correctly. It always ends up in a loop and not identifying the open or closed process and it simply defaults to "Server was not found!". If I swap the code it will simply loop "Server was found!" and proceed that way.
This is not the only example I've tried either. I've looked at literally every article I could find on how to check if a process or window was open. I've tried to add a title to the window and search for the title, I've tried other code configurations and if then statements and nothing works.
In the end I'd like the .bat to simply check if the server process is running or if the window is open, either way as I don't care how it identifies it, and if the server window is open and the server is running then simply recheck every 5 seconds. If it finds that the process or window was closed or ended, then it should close all other .bats and restart the process of initiating the server. It's mainly for when the server crashes I want it to restart if it does.
#echo off
goto check
:check
cls
echo Starting Anit Crash... Check every 5 Seconds...
SETLOCAL EnableExtensions
set EXE=program.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo Server was not found!
echo Server may have crashed...
timeout /t 5
echo Shutting Down Other Processes First...
taskkill /f /im "schedule.bat"
echo Server Restarting...
start cmd /k call initiate.bat
echo Exiting Anti Crash...
timeout /t 3
goto FIN
:FOUND
echo Server Was Found...
echo Server Is Active... Rechecking In 5 Seconds...
timeout /t 5
goto check
:FIN
exit
It may solve your Problem
#echo off
:values
set EXE=safeincloud.exe
:check
cls
echo Starting Anti Crash... Check every 5 Seconds...
tasklist /fi "ImageName eq %EXE%" /fo csv 2>NUL | find /I "%EXE%">NUL
if "%ERRORLEVEL%"=="0" goto FOUND
echo Server was not found!
echo Server may be crashed...
timeout /t 5 >nul
echo Shutting Down Other Processes First...
taskkill /f /im "schedule.bat"
echo Server Restarting...
start "" "initiate.bat"
echo Exiting Anti Crash...
timeout /t 3 >nul
goto End
:FOUND
echo.
echo Server Was Found...
echo Server Is Active... Rechecking In 5 Seconds...
timeout /t 5 >nul
goto check
:End
exit
Source : How to check if a process is running via a batch script

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

How can I stop one instance via cmd of a process when several are running?

I start a program from a scilab script via the command line, start myprog.exe.
After the start my scilab script needs to keep going.
Now I want to stop exactly this instance of the process via the command line too, even if several instances of the same program are running.
Is that possible?
I know how to query via batch files whether a process of this program is running and then stop it, but I don't know how to get the exact allocation.
Is there something like a process id?
I use this to check if the process is running:
tasklist /fi "imagename eq ccx.exe" |find ":" > nul
if errorlevel 1 echo Program is running
if not errorlevel 1 echo Program is not running
Use the command tasklist to view all running tasks with their PID
then
Taskkill /PID 26356 /F
or
Taskkill /IM myprog.exe /F

Windows Command Line wait for task end before next command

I have some software for Windows that opens a website on exit. I want to run a batch that closes the browser after exit from example.exe
example.exe
(wait for end of example.exe)
taskkill /im firefox.exe
taskkill /im iexplore.exe
taskkill /im chrome.exe
The problem you are observing is probably that at the end of your game, the browser is started as another process. It can then take a while before the browser actually starts but the game doesn't wait and just exits. So when you come at the taskkill commands, the browser is still not open. What you need is a way to verify if a browser-process already started, not a way to find out if the exemple.exe (or your game) exited or not. You can do it this way:
:waitOnBrowser
tasklist | findstr /B /C:"chrome.exe" /C:"firefox.exe" /C:"iexplore.exe" > nul
REM the errorlevel will only be 0 if one of the browser has been found in the tasklist
IF ERRORLEVEL 1 goto :waitOnBrowser
REM At this point, you're sure at least one browser has been started
REM taskkill can come here
IF ERRORLEVEL 1 is kind of the same as saying IF %ERRORLEVEL% GTR 1 (see IF in batch).

Executing a command when closing Batch file

I have a batch file playing a sound in the background while it runs, using the method at Batch file executing a sound silently however, when I exit the batch file, the music will continue to play.
Id there any way to run a command while a batch file is closing, so that the sound file will stop playing?
I've tried an example I found called onexit:
onexit taskkill /im wmplayer.exe
But it doesn't work.
Just :
taskkill /f /im wmplayer.exe
Should work.
First idea i had is another batch:
#ECHO OFF
START "" /W <YourBatchhere>
TASKKILL /im "wmplayer.exe"
should do the job :)
The /W will wait at that point, still YourBatchhere has ended.
Regardless of success or error.

Resources