Every now and then (every few days) I notice that a process is using 100% CPU. The process is avrdude
started by the Arduino IDE which, under certain circumstances that I haven't been able to reproduce just sits there at 100% CPU as shown in top
.
Possibly the circumstances are that an upload to the Arduino board commences, and the board is disconnected during the process.
I have 8 cores in the processor, so it isn't immediately obvious that one of them is maxed out. In fact, it only becomes noticeable if it happens a few times in a row, and then I have maybe 3 cores at 100% CPU.
Is there a way of having some background task check for this (say, every 15 minutes), and then alert me in some way (eg. some pop-up dialog)? I am using Ubuntu 14.04 LTS.
Thanks to MelBurslan for his answer, but I'm stumped as to why it isn't fully working. My current script is this:
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
I dropped the threshold down for testing. However as you can see it collects the individual processes OK, but the final test (to display the dialog box) fails because pstring has suddenly become empty for reasons I can't see:
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
stuff | while ... otherstuff ... done
. Pipeline segments including the last at least forbash
are run in subshells, so side-effects in one do not affect the parent shell. In bash (and ksh) you can fix this with process substitution, but in this case one awk can do all of it:top -b -n1 | awk -vthresh=2 'NR>7 && $9>thresh { p=p $12 " " } END { print p }'
or even...| awk -vthresh=2 -vORS=" " 'NR>7 && $9>thresh'; echo