Killing Non-Responding Processes

by Jan 15, 2020

Process objects returned by Get-Process can tell whether the process is currently responding to window messages and thus to user requests. This line takes the current PowerShell process to expose its property “Responding”:

 
PS> Get-Process -Id $Pid | Select-Object *respond*

Responding
----------
      True
 

It is common for a process to occasionally become unresponsive, i.e. because of a high load and weak software architecture. When a process does not respond for a longer period of time, it is known to “hang”, and users get mad.

This line lists processes currently not responding:

 

PS> Get-Process | Where-Object { !$_.Responding }

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                    
-------  ------    -----      -----     ------     --  -- -----------                                                                    
    560      28    21752        580       0.38  18544   1 Calculator                                                                     
    915      65    26660       2528       0.39  14244   1 MicrosoftEdge                                                                  
    488      21     7108       7988       0.09  13400   1 MicrosoftEdgeCP                                                                
    543      27    16148        520       0.31  21200   1 Time                                                                           
   1132      77    63544        836       1.55  15212   1 WinStore.App  
 

Note that this list may include Windows apps that have been started but are no longer in use. Since the property “Responding” is just describing the current status, it does not tell for how long the process was not responding.

If you wanted to report (or kill) all processes not responding for some period of time, you need to conduct repeated checks yourself and keep track of the results.

The code below lists all processes not responding for 3 seconds in a grid view window. The user can then select one or more (hold CTRL to select multiple processes) processes to kill.

# report processes hanging for more than 3 seconds
$timeout = 3

# use a hash table to keep track of processes
$hash = @{}

# use an endless loop and test processes
do
{
  Get-Process |
  # look at processes with a window only
  Where-Object MainWindowTitle |
  ForEach-Object {
    # use process ID as key to the hash table
    $key = $_.id
    # if the process is responding, reset the counter
    if ($_.Responding)
    {
      $hash[$key] = 0
    }
    # else, increment the counter by one
    else
    {
      $hash[$key]++
    }
  }
    
  # copy the hash table keys so that the collection can be 
  # modified
  $keys = @($hash.Keys).Clone()
   
  # emit all processes hanging for longer than $timeout seconds
  # look at all processes monitored
  $keys |
  # take the ones not responding for the time specified in $timeout
  Where-Object { $hash[$_] -gt $timeout } |
  ForEach-Object {
    # reset the counter (in case you choose not to kill them)
    $hash[$_] = 0
    # emit the process for the process ID on record
    Get-Process -id $_
  } | 
  # exclude those that already exited
  Where-Object { $_.HasExited -eq $false } |
  # show properties
  Select-Object -Property Id, Name, StartTime, HasExited |
  # show hanging processes. The process(es) selected by the user will be killed
  Out-GridView -Title "Select apps to kill that are hanging for more than $timeout seconds" -PassThru |
  # kill selected processes
  Stop-Process -Force

  # sleep for a second
  Start-Sleep -Seconds 1

} while ($true)

You can easily change the code of course to have it report hanging processes. Simply replace Stop-Process with whatever you want to do, i.e. use Add-Content to write the processes to a log file. To not log the same processes over and over again, you might want to add some sort of blacklist that keeps track of processes already logged.


Twitter This Tip! ReTweet this Tip!