0

I am trying to write a batch script that detects if an .exe is not responding, and if it isn't, it will run a piece of code that kills it and then does some other things as well. I know how I can kill the process and restart it if it is not responding in one line, but I am not sure how I can do more than just restart it by converting this into an if statement, or calling a goto.

taskkill /im "exeName.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && start "" "pathToExe"

I have come across other Stack Overflow posts that are similar to this, however they only check the error level of the process and do not check if the program is not responding and how to perform code after that.

How would I go about this? Thanks in advance.

Luke
  • 130
  • 1
  • 9
  • 1
    Possible duplicate of [check if command was successful in a batch file](https://stackoverflow.com/questions/14691494/check-if-command-was-successful-in-a-batch-file) – Ken Y-N Dec 27 '18 at 01:16
  • 1
    @KenY-N That does not check if the task is not responding. That only checks if there are other errors such as if it is not currently running. – Luke Dec 27 '18 at 01:21
  • Is the problem that you want something like "if not responding then do something other than kill the process"? – Ken Y-N Dec 27 '18 at 02:28

2 Answers2

0

Assuming I am interpreting your question correctly and you want to test without killing a task, how about this:

tasklist /fi "status eq not responding" /nh | find "exeName.exe" >NUL
if %errorlevel% == 0 (
    echo Oops, we've hung...
)

tasklist takes the same /fi status options as taskkill, even though the documentation states only RUNNING is allowed - taskkill /? on Windows 8 at least shows the full options. We then use find to check that the executable is in the list of not responding tasks.

BTW, if you want to use PowerShell instead:

$foo = get-process exeName
if (!$foo.Responding) {
    echo "Oops, we've hung..."
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
0

Can be done more easily:

taskkill /FI "STATUS eq NOT RESPONDING" /IM "yourexe.exe" /F | findstr /c:"SUCCESS" >nul
if %errorlevel% EQU 0 (echo Successfully detected and terminated "yourexe.exe" which didn't respond) else (echo Ooops! We didn't find or could not terminate process "yourexe.exe")

If you want to just detect if process is not responding, then use tasklist:

tasklist /FI "STATUS eq NOT RESPONDING" | findstr /c:"yourexe.exe">nul
if %errorlevel% EQU 0 (echo We have detected that process with image name "yourexe.exe" is not responding.) else (echo We didn't found process with image name "yourexe.exe" because it doesn't exist.)

In both cases we use findstr command because even if process not found/terminated taskkill/tasklist will return errorlevel 0.

double-beep
  • 5,031
  • 17
  • 33
  • 41