How do I log CPU usage per process?

Solution 1:

You could try to do something like this:

while true; do ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10 >> logfile.txt; printf "\n" >> logfile.txt; sleep 3; done

that would show you the top ten processes in terms of CPU usage. You can change the number of processes shown by changing the 10 in "head -10" to a different number, and how often it updates by changing the 3 in "sleep 3" or taking out the "sleep 3" part entirely.

Solution 2:

Check out atop it will write a binary log of pretty much everything you would possibly want and then you can use a top like gui to go through the time slices of the day (default is to take the data every 5 minutes). http://www.atcomputing.nl/Tools/atop/


Solution 3:

I think that munin is one of the goods tools of monitoring that will help your to get some information about your box's activities. Also, there are some command line tool like sar, iostat, ps, top for such use.


Solution 4:

The other answers have only shown you how you can look at what's currently going on, which doesn't help if the system has been rebooted.

If you want this information recorded for posterity (or billing, or whatever other use you might also have), what you want is process accounting.

Here's a HOWTO I found, but I'll be honest -- it's been a decade since I've used process accounting.

http://tldp.org/HOWTO/Process-Accounting/


Solution 5:

A more user-friendly approach to shawn's solution for near real-time monitoring:

while true; do clear; ps -eo pcpu,pmem,pid,user,args --sort=-pcpu c|head -20; sleep 1; done

This will provide a static view of the top 20 processes that will be refreshed every 1 sec. The "c" option in the ps command will print the process executable name rather than the whole args command. You can omit this option if you need the whole command info instead. %memory usage column also added.