web-dev-qa-db-ja.com

アプリケーションが応答していないかどうかを確認するにはどうすればよいですか?

OSXにアプリケーションがあり、繰り返し無応答状態になり、強制終了する必要があります。自動化することを望んでいましたが、psでプロセスを検査すると、NotResponding状態に対応するものが見つかりません。 stateインジケーターを見ましたが、アプリは応答しているかどうかにかかわらず[〜#〜] s [〜#〜]と表示されます。

state状態は、「RWNA」などの一連の文字で指定されます。最初の文字は、プロセスの実行状態を示します。

  • アイドル状態(約20秒以上スリープ)のプロセスをマークします。
  • R実行可能なプロセスをマークします。
  • S約20秒未満スリープしているプロセスをマークします。
  • T停止したプロセスをマークします。
  • U無停電待機中のプロセスをマークします。
  • Zデッドプロセス(「ゾンビ」)をマークします。

アクティビティマネージャーのように、プロセスが応答なしであるかどうかを確認するにはどうすればよいですか?


私はAppleScriptソリューションにもオープンです。

11
C. Ross

Not Responding状態はプロセス状態ではなく、プロセスがウィンドウマネージャー/グラフィカルエンジンとの通信を停止しました。それはループに縛られ、ソケット、リモートファイル、イベントを処理するメインループに戻り続けるものすべてにぶら下がっている可能性があります。ウィンドウマネージャは、イベントがキューに入れられていることに気づき、「応答していません」というラベルを付けます

プロセスにダミーイベントを送信する小さなX11プログラムを作成し、応答しない場合は強制終了する必要がある場合があります。

9
JvO

これは、応答しないプロセスを探してそれらを強制終了するUIスクリプトを使用するAppleScriptです。

マーベリックスのアクティビティモニターで動作します。ただし、これはUIスクリプトであり、アクティビティモニターのUIが変更されているため、マイナーな変更を加えないと、古いOSXでは機能しない可能性があります。

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

(コメントに入れるには長すぎるため、これを別の回答として投稿する)

元のスクリプトについては、@ MatthieuRieglerの功績によるものです。

これは10.12.6で機能し、元のスクリプトのマイナーな変更です(私が独自の調査を行った後、@ CharlieGorichanazのコメントを見ました):


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 macOS 10.12.xでは、ツールバーに追加の enter image description here ボタンのセット(CPU、メモリ、エネルギーなど)がgroup 2 of toolbar 1ではなくgroup 1 of toolbar 1にあるためのアイコン。そのアイコンがない場合(古いmacOSバージョンでは確認していません)、CPUなどのボタンはgroup 1 of toolbar 1にあると思います

** 2 これは、アクティビティ列のPID列を別の位置にドラッグしたことがある場合に適用されます。 PID列を左端の位置にドラッグしたので、この行で、インデックスを1に変更する必要がありました。

set pid to value of text field 1 of aProcess

列には、左端から1から始まる番号が付けられています。したがって、必要に応じて、上記の行で強調表示されているインデックスを適宜調整してください。

0
Ashutosh Jindal