12

I have an application on OSX that repeatedly goes into a Not Responding state and has to be force killed. I was hoping to automate it, but when inspecting the process with ps I don't see anything that corresponds with the Not Responding state. I looked at the state indicator, but the app shows as S whether it is responding or not.

state The state is given by a sequence of characters, for example, ``RWNA''. The first character indicates the run state of the process:

  • I Marks a process that is idle (sleeping for longer than about 20 seconds).
  • R Marks a runnable process.
  • S Marks a process that is sleeping for less than about 20 seconds.
  • T Marks a stopped process.
  • U Marks a process in uninterruptible wait.
  • Z Marks a dead process (a ``zombie'').

How can I determine if the process is Not Responding as the Activity Manager does?


I am open to AppleScript solutions as well.

3 Answers 3

9

The Not Responding state is not a process state, but rather the process has stopped communicating with the window manager / graphical engine. It could be tied up in a loop, hanging on a socket, remote file, anything that keeps it returning to the main loop that handles events. The window manager notices events are being queued up and thus labels it as "Not responding"

You may need to write a small X11 program that sends dummy events to the process, then kill it if it doesn't respond.

3
  • Maybe writing something with AppleScript that as UI level access. Dec 13, 2013 at 17:36
  • @MatthieuRiegler How would you do that in AppleScript?
    – C. Ross
    Dec 14, 2013 at 1:10
  • I provided an example in an other answer. Dec 14, 2013 at 2:23
4

Here is an AppleScript using UI scripting that looks for a not responding process and kills them.

It will work with the Activity Monitor of Mavericks. But since this is UI scripting and since the UI of Activity Monitor changed, this will most likely not work with older OS X without some minor modifications.

tell application "Activity Monitor" to run  --We need to run Activity Monitor
tell application "System Events" to tell process "Activity Monitor"
    tell radio button 1 of radio group 1 of group 1 of toolbar 1 of window 1 to click --Using the CPU View 
    tell outline 1 of scroll area 1 of window 1 -- working with the list 
        set notResponding to rows whose value of first static text contains "Not Responding" -- Looking for Not responding process
        repeat with aProcess in notResponding
            set pid to value of text field 5 of aProcess  -- For each non responding process retrieve the PID 
            if pid is not "" then do shell script ("kill -9 " & pid) -- KILL the PID. 
        end repeat
    end tell
end tell
4
  • I get a compile error on the line tell radio button 1 of radio. I removed that and tweaked some other stuff (I only want to kill a specific program) and get the runtime error: 'error "System Events got an error: Access for assistive devices is disabled." number -1719 from window 1 of process "Activity Monitor"'
    – C. Ross
    Dec 14, 2013 at 12:13
  • Did run that script on OSX Mavericks ? Dec 14, 2013 at 18:10
  • OSX 10.8, so no.
    – C. Ross
    Dec 14, 2013 at 20:09
  • worked on 10.12.5 after changing to tell radio button 1 of radio group 1 of group 2 of toolbar 1 of window 1 to click Nov 1, 2017 at 6:25
0

(Posting this as a separate answer since too long to fit in a comment)

Credit to @MatthieuRiegler for the original script.

This worked on 10.12.6 and is a minor modification of the original script (saw @CharlieGorichanaz's comment after I'd done my own investigation):


set textToSearchForInProcessName to "Not Responding"

--  Run Activity Monitor 
tell application "Activity Monitor" to activate

tell application "System Events" to tell process "Activity Monitor"
    --  Wait for the Activity Monitor window to open
    repeat until (exists window 1)
        delay 1
    end repeat
    --display notification "Window appeared"

    --  Wait for the Menubar to be present
    repeat until (exists menu 1 of menu bar item "View" of menu bar 1)
        delay 1
    end repeat
    --display notification "Menubar appeared"

    --  Make sure View -> My Processes is selected 
    click menu item "My Processes" of menu 1 of menu bar item "View" of menu bar 1

    --  Click the 'CPU View' button  ( **1 ) 
    click radio button 1 of radio group 1 ¬
        of group 2 of toolbar 1 ¬
        of window 1

    --  Working with the list of processes 
    tell outline 1 of scroll area 1 of window 1
        --  Looking for Not responding process  
        set notResponding to rows whose value of ¬
            first static text contains textToSearchForInProcessName

        repeat with aProcess in notResponding

            --  For each non responding process retrieve the PID 
            set pid to value of text field 1 of aProcess -- ( **2 )

            --  Kill that process using pid 
            if pid is not "" then do shell script ("kill -9 " & pid)
        end repeat
    end tell
end tell

**1 In macOS 10.12.x, the toolbar contains an additional enter image description here icon due to which the set of buttons (CPU, Memory, Energy, etc) are in group 2 of toolbar 1 instead of group 1 of toolbar 1. In absence of that icon (I haven't confirmed in older macOS versions), I believe the CPU etc buttons would be in group 1 of toolbar 1

**2 This applies if you've ever dragged the PID column in Activity column to a different position. I'd dragged the PID column to the leftmost position so on this line, I had to change the index to 1 :

set pid to value of text field 1 of aProcess

The columns are numbered from the leftmost, starting at 1. So adjust the highlighted index in the above line accordingly if needed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .