This will append the Top 10 most CPU hungry processes to a file ps.log every five seconds.
Note that this is not the full boat-load of information top would 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.
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).
This works very well:
This 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):
This will append the Top 10 most CPU hungry processes to a file
ps.log
every five seconds.Note that this is not the full boat-load of information
top
would 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
ps.log
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
:This 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 usingapt-get
)I`ve got an script for get the process load, with rotating logs:
My original source can be browsed here
Here's a logging script I wrote and use: cpu_logger.py. It logs continuously to a set of rotating log files. It allows me to write reports to Google like this: Chrome Google Meet CPU usage is still too high (see plots and CPU usage during Google Meet with my camera ON vs OFF).
You can also use cpu_load.py. Example run and output of
cpu_load
:See also my answer here: Stack Overflow: How to get overall CPU usage (e.g. 57%) on Linux and my Q&A here: Unix & Linux: How to get overall CPU usage (e.g. 57%) on Linux.