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

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.
LVL 2
aladin404Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kamaraj SubramanianApplication Support AnalystCommented:
while(1)
do
top -b -n 1 | awk ' /^[0-9]/ {if (substr($9,1,length($9)-1) > 50) {print $0}}' > /tmp/process.log
sleep 3
done

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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amit_panjwaniCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.