如何偵測並警告某個進程是否長時間使用100% CPU?

如何偵測並警告某個進程是否長時間使用100% CPU?

我時不時地(每隔幾天)注意到某個進程正在使用 100% CPU。這個過程avrdude由 Arduino IDE 啟動,在某些情況下,我無法重現該 IDE,它的 CPU 使用率僅為 100%,如圖所示top

可能的情況是,開始上傳到 Arduino 板,並且在此過程中板斷開連接。

我的處理器有 8 個核心,因此不會立即明顯看出其中一個核心是否已滿。事實上,只有當它連續發生幾次時,它才會變得明顯,然後我可能有 3 個核心,CPU 使用率為 100%。

有沒有辦法讓一些後台任務對此進行檢查(例如,每 15 分鐘),然後以某種方式提醒我(例如,一些彈出對話框)?我正在使用 Ubuntu 14.04 LTS。


感謝 MelBurslan 的回答,但我很困惑為什麼它沒有完全發揮作用。我目前的腳本是這樣的:

cpupercentthreshold=2
pstring=""
top -b -n 1 | sed -e "1,7d" | while read line; do
cpuutil=$(echo ${line} | awk '{print $9}' | cut -d"." -f 1)
procname=$(echo ${line} | awk '{print $12}' )
if [ ${cpuutil} -ge ${cpupercentthreshold} ]
then
  echo ${cpuutil}
  pstring=${pstring}${procname}" "
  echo pstring is currently ${pstring}
fi
done
echo pstring is ${pstring}
if [ -n "${pstring}" ]
then
  zenity --title="Warning!" --question --text="These processes are above CPU threshold limit ${pstring}" --ok-label="OK"
fi

我降低了測試的門檻。然而,正如您所看到的,它收集了各個進程,但最終測試(顯示對話框)失敗了,因為 pstring 由於我看不到的原因突然變空:

13
pstring is currently VirtualBox
6
pstring is currently VirtualBox Xorg
6
pstring is currently VirtualBox Xorg compiz
6
pstring is currently VirtualBox Xorg compiz ibus-engin+
6
pstring is currently VirtualBox Xorg compiz ibus-engin+ top
pstring is

答案1

在閱讀了 MelBurslan 的答案和各種評論後,我決定嘗試(受到他們建議的啟發)用 Lua 做一個版本。這是在盧阿5.1.5- 我不確定它是否適用於最新的 Lua。

總體思路是使用Lua的popen(打開一個管道)來執行top,然後使用正規表示式(或圖案,正如 Lua 中所說的那樣)。然後考慮匹配線(其中大部分)是否超過閾值百分比。如果這樣做,它們將被添加到表中。

如果表不為空,則zenity呼叫 then 向使用者顯示一則訊息。我在開發過程中發現的一些「陷阱」:

  • 我在 zenity 中添加了 60 秒的超時,這樣,如果您當時不在電腦旁,就不會出現警告對話框。
  • 我添加了--display=:0.0以便在運行時找到顯示螢幕cron
  • 我在 crontab 中簡化了「每 15 分鐘」的測試,如下所示:

    */15 * * * * /home/nick/check_cpu_usage.lua
    
  • 正規表示式會捕獲所有內容,以防top您想做其他測試(例如,使用太多記憶體)。

我認為這比啟動大量進程和子 shell 更快。似乎工作正常。透過降低閾值(例如降低到 5)並更改 crontab 條目以每分鐘檢查一次來進行測試。


檢查CPU使用情況.lua

#! /usr/local/bin/lua

THRESHOLD = 90  -- percent

-- pipe output of top through a file "f"
f = assert (io.popen ("top -b -n 1 -w 512"))
t = { }

-- check each line
for line in f:lines() do

  -- match top output, eg.
  --   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  -- 30734 nick      20   0 6233848 3.833g 3.731g S   8.6 12.2   3:11.75 VirtualBox

  local pid, user, priority, nice, virt, res, shr, 
        status, cpu, mem, time, command =
    string.match (line,
      "^%s*(%d+)%s+(%a+)%s+(%-?%d+)%s+(%-?%d+)" ..
--         pid      user   priority    nice
      "%s+([%d.]+[g]?)%s+([%d.]+[g]?)%s+([%d.]+[g]?)%s+([DRSTZ])%s+(%d+%.%d+)%s+(%d+%.%d+)" ..
--        virtual          res           shr             status       %cpu        %mem
      "%s+([0-9:.]+)%s+(.*)$")
--         time       command

  -- if a match (first few lines won't) check for CPU threshold
  if pid then
    cpu = tonumber (cpu)
    if cpu >= THRESHOLD then
      table.insert (t, string.format ("%s (%.1f%%)", command, cpu))
    end -- if
  end -- if

end -- for loop

f:close()

-- if any over the limit, alert us
if #t > 0 then
  os.execute ('zenity --title="CPU usage warning!" --info ' ..
              '--text="These processes are using more than ' ..
              THRESHOLD .. '% CPU:\n' ..
              table.concat (t, ", ") ..
              '" --ok-label="OK" ' ..
              '--timeout=60 ' ..   -- close dialog after one minute in case we aren't around
              '--display=:0.0 '  -- ensure visible when running under cron
              )
end -- if

答案2

建立一個像這樣的簡單腳本

cpupercentthreshold=75
pstring=""
top -b -n 1 | sed -e "1,7d" | while read line; do
cpuutil=$(echo ${line} | awk '{print $9}' | cut -d"." -f 1)
if [ ${cpuutil} -ge ${cpupercentthreshold} ]
then
  pstring=${pstring}${procname}" "
fi
done
if [ -z "${pstring}" ]
then
  echo "Everything looks good $(date)" >>mylogfile #if you want to keep track 
else
  zenity --title="Warning!" --question --text="These processes are above CPU threshold limit ${pstring}" --ok-label="OK"
fi

然後運行命令crontab -e並插入如下行:

0,15,30,45 * * * * /path/to/my/checker/script 

儲存並退出。然後執行

chmod 755 /path/to/my/checker/script

我不太熟悉,zenity因為我已經有一段時間沒有在 Linux 伺服器上使用圖形顯示了,也從來不需要使用它。因此,如果由於某種原因失敗,請在 上尋求協助man zenityxdialog據我所知,它是舊版可執行檔的替代品。

相關內容