Linux – How to Log CPU Usage Per Process

central-processing-unitlinuxloggingprocess

I have a box on Linode that's going through weird behavior. Every now and then CPU and disk I/O will shoot to 100% and the server becomes unresponsive and has to be booted. I'd like to investigate better what's going on, but I don't know how to find who's responsible for all that CPU and I/O. I'm running Gentoo 2.6.18.

Best Answer

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.

Related Topic