Sometimes a game becomes unresponsive and I have to go into Task Manager and kill it, then restart it. It can be very annoying and time-consuming. While I know how to use taskkill and start an app through batch files, I do not know how to test if an application is unresponsive. I also don't know the proper way to have it run in the background and not use too many resources. How do I do this for either a specific executable or any process?
- 4,816
- 21
1 Answers
Warning
This is technically possible, but may not necessarily be a smart idea for the following reasons:
- Sometimes processes will stop responding when there is a lot of data to process and may recover after a little while.
- Killing a process without giving it a chance to recover can cause loss of data or other issues.
Script
That being said, I think the following batch file might work for very simple cases. It allows you to wait some time before killing the application.
@ECHO off
SET _AppLocation=C:\Path\To\Exe
SET _Application=EXCEL.EXE
SET _Parameters=
SET _Delay=5
:Start
SET _NotResponding=FALSE
SET _PID=FALSE
FOR /F "tokens=1,2 delims=," %%a IN ('tasklist /fi "STATUS eq NOT RESPONDING" /fi "IMAGENAME eq %_Application%" /NH /FO CSV ^| FIND "%_Application%"') DO SET _NotResponding=TRUE & SET _PID=%%~b
IF %_NotResponding%==TRUE (
ECHO %_Application% ^(%_PID%^) is not responding. Waiting %_Delay% seconds...
TIMEOUT /T %_Delay%
ECHO Killing and restarting...
FOR /F %%a IN ('tasklist /fi "STATUS eq NOT RESPONDING" /fi "PID eq %_PID%" /NH /FO CSV') DO TASKKILL /F /fi "PID eq %_PID%"
%_AppLocation%%_Application% %_Parameters%
)
TIMEOUT /T 1 >nul
GOTO:Start
Explanation
Configuration:
_Applocationis the full directory path to the application. You can't get this from theTASKLISTcommand and it's necessary for restarting the application._Applicationis the full filename, including extension._Parametersis all of the parameters needed to start the application. You can't get this from theTASKLISTcommand and it's necessary for restarting the application._Delayspecifies the amount of seconds to wait for the application to recover.
The Loop:
:Startsets a starting point for this infinite loop we're creating.SET _NotResponding=FALSEandSET _PID=FALSEare used to store information we need to be able to wait for a bit for the application to recover.tasklist /fi "STATUS eq NOT RESPONDING" /fi "IMAGENAME eq %_Application%" /NH /FO CSV ^| FIND "%_Application%"will return some information on the application if it is not responding. (If you want to use this outside of theFORloop, remove the^character.)If the application isn't running or is running and responding, this won't return anything, so the for loop will have nothing to process and will exit.
FOR /F "tokens=1,2 delims=," %%a IN ('...') DOwill process each line of the output from theTASKLISTfunction, but only the last line will be stored for later processing. If you have multiple instances of the same application running and they all stop responding, this will only handle one of them.SET _NotResponding=TRUE & SET _PID=%%~bwill store the process ID and let the next section of the script know to run.IF %_NotResponding%==TRUEwill run the code inside the parentheses()only if there is an unresponsive instance of the application.TIMEOUT /T %_Delay%will wait the specified amount of seconds before deciding the application is unrecoverable.tasklist /fi "STATUS eq NOT RESPONDING" /fi "PID eq %_PID%" /NH /FO CSVwill check to see if that process ID is still not responding.TASKKILL /F /fi "PID eq %_PID%"will forcefully kill the process if it is still not responding.%_AppLocation%\%_Application% %_Parameters%will restart the application.TIMEOUT /T 1will wait a second so that this batch file doesn't check constantly.GOTO:Startwill return to the beginning of the loop and perform all of these steps again.
To exit this infinite loop, you will need to either close the console running the script or press Ctrl + C
Further Reading
- 4,816