How to log CPU load?
Matthew Barrera
How to log CPU load to a file in order to investigate a problem?
35 Answers
This works very well:
while true; do uptime >> uptime.log; sleep 1; doneThis will log your cpu load every second and append it to a file
uptime.log.You can then import this file into Gnumeric or the OpenOffice spreadsheet to create a nice graph (select 'separated by spaces' on import).
As Scaine noticed, this won't be enough to diagnose the problem. So, additionally, run this (or use his answer for this part):
while true; do (echo "%CPU %MEM ARGS $(date)" && ps -e -o pcpu,pmem,args --sort=pcpu | cut -d" " -f1-5 | tail) >> ps.log; sleep 5; doneThis will append the Top 10 most CPU hungry processes to a file
ps.logevery five seconds.Note that this is not the full boat-load of information
topwould give you. This is just the top 10, and just their CPU Usage, Memory Usage and the first argument (i.e. their command without further arguments, as in/usr/bin/firefox)
After you've used a Spreadsheet to create a graph to see when your CPU load went through the roof, you can then search this file for the nearest time to see what process has caused it.
This is what those files will look like:
uptime.log
~$ cat uptime.log 22:57:42 up 1 day, 4:38, 4 users, load average: 1.00, 1.26, 1.21 22:57:43 up 1 day, 4:38, 4 users, load average: 0.92, 1.24, 1.21 22:57:44 up 1 day, 4:38, 4 users, load average: 0.92, 1.24, 1.21 22:57:45 up 1 day, 4:38, 4 users, load average: 0.92, 1.24, 1.21 ...ps.log
%CPU %MEM ARGS Mo 17. Jan 23:09:47 CET 2011 0.7 0.9 /usr/bin/compiz 0.8 0.5 /usr/lib/gnome-panel/clock-applet 1.1 1.7 /opt/google/chrome/chrome 1.2 0.3 /usr/bin/pulseaudio 1.8 4.0 /opt/google/chrome/chrome 2.6 1.5 /opt/google/chrome/chrome 2.6 3.2 /usr/bin/google-chrome 3.6 2.6 /opt/google/chrome/chrome 4.9 1.5 /usr/bin/X 5.7 1.6 /opt/google/chrome/chrome
%CPU %MEM ARGS Mo 17. Jan 23:09:48 CET 2011 0.7 0.9 /usr/bin/compiz 0.8 0.5 /usr/lib/gnome-panel/clock-applet 1.0 1.7 /opt/google/chrome/chrome 1.2 0.3 /usr/bin/pulseaudio 1.8 4.0 /opt/google/chrome/chrome 2.6 1.5 /opt/google/chrome/chrome 2.6 3.2 /usr/bin/google-chrome 3.6 2.6 /opt/google/chrome/chrome 4.9 1.5 /usr/bin/X 5.7 1.6 /opt/google/chrome/chrome ... 4 You can run the top command in batch mode by using the -b option, then dump that to a file.
On start up of your PC, open a terminal, run
top -b > ~/cpu.txt
Then when your PC freezes, simply open the (probably huge) text file and check the last entry for some detail on what was running just before the crash. In the fact the file will be so stupidly large that you're better off running a tail -250 ~/cpu.txt instead.
Also check your /var/log/kern.log in case your issue is hardware related (unlikely if this is only happening after an upgrade, but worth checking nonetheless).
I found a great answer by Christopher to this question on Unix and Linux that uses top:
top -n 1 -b > top.outThis will give you 1 iteration of top then stop, and then push it to a file.
For those who need to run this command after your putty ( SSH client ) session end. you can use command screen ( or install it using apt-get )
I`ve got an script for get the process load, with rotating logs:
#!/bin/bash
MaxFileSize=204800
DaysToKeep=7
echo -e "\n Fecha:"`date` >> /var/log/ps.log
echo -e "\n Uptime: "`uptime` >> /var/log/ps.log
ps -e -o pcpu,pmem,args --sort=pcpu | tail >> /var/log/ps.log
#Get size in bytes**
file_size=`du -b /var/log/ps.log | tr -s '\t' ' ' | cut -d' ' -f1`
if [ $file_size -gt $MaxFileSize ];then timestamp=`date +%s` mv /var/log/ps.log /var/log/ps.log.$timestamp gzip /var/log/ps.log.$timestamp touch /var/log/ps.log # remove old files find /var/log -name "ps.log.*" -type f -mtime +$DaysToKeep -delete
fiMy original source can be browsed here