Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

log processes with high CPU usage?

Hello,

I have a Ubuntu 12.04 machine. At times I see that there are some processes that are consuming very high CPU capacity. I was wondering if there is a script I can use that runs in the background on my machine that will log all the processes that consume more than 50% of the CPU capacity at any given time and log the output to a text file to review? Thank you.

A
Avatar of farzanj
farzanj
Flag of Canada image

One way could put some command like the following in your cron.

ps ahkpcpu -o"%C%c" | while read c a;do if [[ `echo "$c >= 50" | bc` == 1 ]] ; then echo "$c $a"; fi; done >> /path/file

Open in new window



Although you man want to explore tools like sar that do historical record keeping.
Avatar of aej1973
aej1973

ASKER

farzanj, thank you. The thing with sar is that, unlike top, it shows the CPU utilization but does not show which process has taken up the CPU capacity, am I right or am I missing something? Thank you.
Not sure if you want the process id (PID) or the actual command, but to do both, just add in %p (process id) and %a (command) to the ps output options:
ps ahkpcpu -o"%p%C%c%a"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aej1973

ASKER

Thank you.