時々 (数日おきに)、プロセスが CPU を 100% 使用していることに気が付きます。このプロセスはavrdude
Arduino IDE によって開始されますが、再現できなかった特定の状況下では、 に示すように CPU を 100% 使用したままになりますtop
。
おそらく、Arduino ボードへのアップロードが開始され、そのプロセス中にボードが切断される状況です。
プロセッサには 8 つのコアがあるため、そのうちの 1 つが最大限に使用されていることはすぐにはわかりません。実際、数回連続して発生した場合にのみ顕著になり、その場合、CPU 使用率が 100% になっているコアは 3 つ程度になります。
バックグラウンド タスクでこれをチェックし (たとえば、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
ユーザーにメッセージを表示するために が呼び出されます。開発中に私が見つけたいくつかの「落とし穴」は次のとおりです。
- zenity に 60 秒のタイムアウトを追加したので、その時点で PC の前にいなかった場合、画面に警告ダイアログが表示されなくなりました。
--display=:0.0
で実行したときに表示画面が見つかるように追加しましたcron
。crontab で「15 分ごと」のテストを次のように簡略化しました。
*/15 * * * * /home/nick/check_cpu_usage.lua
正規表現は、
top
他のテストを実行したい場合(たとえば、メモリの使用量が多い場合)に備えてすべてをキャプチャします。
これは、多数のプロセスやサブシェルを起動するよりも高速だと思います。問題なく動作するようです。しきい値を下げて (たとえば 5 に) テストし、crontab エントリを 1 分ごとにチェックするように変更します。
チェック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 zenity
これは従来の実行可能ファイルの代わりだそうです。xdialog