
我知道我可以做ps -aux | xargs kill
等等,但我想列出所有沒有回應的任務的 PID(活動監視器中的紅色)。我可以在終端機中執行什麼命令來列出這些紅色、無回應的任務並自動終止它們。
答案1
有人已經問了幾乎同樣的問題如何確定應用程式是否沒有回應?(確定哪些進程沒有回應是困難的部分,殺死它們非常容易)。我在這裡引用了一個相關的答案:
「無回應」狀態不是進程狀態,而是進程已停止與視窗管理器/圖形引擎通訊。它可以被綁在一個循環中,掛在套接字上,遠端檔案上,任何讓它回到處理事件的主循環的東西。視窗管理器注意到事件正在排隊,因此將其標記為“未回應”
您可能需要編寫一個小型 X11 程序,將虛擬事件傳送到進程,然後在它沒有回應時終止它。
因此,不可能確定哪些程式沒有回應,至少在不大量依賴 AppleScript/X11 邏輯的情況下是不可能的。
如果您好奇,AppleScript 的這個小片段(為 Mavericks 製作,可能不適用於其他任何東西)也發佈在鏈接的線程上,基本上識別所有無響應的程序,然後向KILL
它們發送信號:
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
但是,如果您發現某個應用程式正在運行,您可以使用 殺死它的所有實例sudo killall [AppName]
,例如sudo killall "Activity Monitor"
。您可以使用 例如 來識別各個應用程式的 PID pgrep [AppName]
,pgrep "Google Chrome"
並且可以使用 來終止任何產生的 PID kill [PID]
。
答案2
雖然有點晚了,但我已經編寫了一個終端應用程式來為您完成此操作。它無需為活動監視器編寫 UI 腳本,而是使用 spindump 報告來確定無回應的進程並自動終止它們。
答案3
它困擾著我的 Jenkins OSX 機器,有時我似乎有一些東西堵塞了我的系統。以便能夠獲得有關任何問題的更好詳細資訊。我會嘗試這個,帽子提示@coldlogic關於使用spindump的想法!
$ sudo spindump -notarget 5 -timelimit 60 -stdout -noFile -noProcessingWhileSampling -aggregateStacksByProcess | grep -B 8 "Unresponsive for"
Sampling all processes for 5 seconds with 10 milliseconds of run time between samples
Sampling completed, processing symbols...
Process: System Preferences [31152]
Path: /Applications/System Preferences.app/Contents/MacOS/System Preferences
Architecture: x86_64
Parent: launchd [1]
UID: 982457465
Task size: 38.81 MB (-20 KB)
CPU Time: <0.001s (263.8K cycles, 65.8K instructions, 4.01c/i)
Note: Unresponsive for 2258 seconds before sampling
今天我能夠重現導致 Sys prefs 掛起的情況,因此我能夠將其用作測試。願這對未來的我或其他人有幫助!
答案4
感謝 Coldlogic。