We help IT Professionals succeed at work.

Linux script ,CPU monitor, make log if process consumes more that 50 % of CPU

aladin404 asked
Hi guys can you help me to make a Linux script which will monitor CPU consumption by system processes, and if some of them consumes more that 50 % of CPU capacity to write that process into the log with precise time and date.

Thank you all for your time.
Watch Question

Application Support Analyst
top -b -n 1 | awk ' /^[0-9]/ {if (substr($9,1,length($9)-1) > 50) {print $0}}' > /tmp/process.log
sleep 3

Open in new window

the code will execute the top command for each 3 seconds and capture the 9th column (assuming it is cpu usage ) and check whether it is 50% or not.

if yes, then log to /tmp/process.log
actually above top can be bit misleading.

top is going to display "total" load in case of multiple cpu's.

I do not know if original poster actually wants combined load on all cpu's or just 1 cpu.

Consider a situation where server has multiple cores/cpu's (say 4)  - and one process utilizes 100% of  cpu1 where as other cpu's might have lesser load or be idle - now the combined average may well be below 50% .

Does original poster wants monitor cpu utilization as whole or just individual pc