2

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?

1 Answer 1

0

Warning

This is technically possible, but may not necessarily be a smart idea for the following reasons:

  1. Sometimes processes will stop responding when there is a lot of data to process and may recover after a little while.
  2. 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:

    • _Applocation is the full directory path to the application. You can't get this from the TASKLIST command and it's necessary for restarting the application.
    • _Application is the full filename, including extension.
    • _Parameters is all of the parameters needed to start the application. You can't get this from the TASKLIST command and it's necessary for restarting the application.
    • _Delay specifies the amount of seconds to wait for the application to recover.
  • The Loop:

    • :Start sets a starting point for this infinite loop we're creating.

    • SET _NotResponding=FALSE and SET _PID=FALSE are 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 the FOR loop, 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 ('...') DO will process each line of the output from the TASKLIST function, 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=%%~b will store the process ID and let the next section of the script know to run.

    • IF %_NotResponding%==TRUE will 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 CSV will 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 1 will wait a second so that this batch file doesn't check constantly.

    • GOTO:Start will 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

1
  • Thanks, I'll try that.
    – Simon Fox
    May 23, 2019 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.