가끔(며칠마다) 프로세스가 CPU를 100% 사용하고 있는 것을 발견합니다. 이 프로세스는 avrdude
Arduino IDE에 의해 시작되는데, 재현할 수 없는 특정 상황에서는 그림과 같이 CPU가 100%로 유지됩니다 top
.
Arduino 보드에 대한 업로드가 시작되고 프로세스 중에 보드 연결이 끊어지는 상황일 수 있습니다.
프로세서에 8개의 코어가 있으므로 그 중 하나가 최대치인지 즉시 알 수 없습니다. 실제로 이러한 현상이 몇 번 연속으로 발생하고 100% CPU에서 3개의 코어가 있는 경우에만 눈에 띄게 됩니다.
이에 대해 백그라운드 작업을 확인하고(예: 15분마다) 어떤 방식으로든(예: 팝업 대화 상자) 알림을 보내는 방법이 있습니까? 우분투 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 항목을 변경하여 매 분마다 확인하세요.
check_cpu_usage.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
내가 들었던 대로 이는 레거시 실행 파일을 대체하는 것입니다 .