How to Find Linux CPU Usage

November 29, 2010

To find what is soaking up your cpu

How do I Find Unix-Linux CPU Utilization

System Monitor

on your desktop, you can find all that and more at
Applications -> System Tools -> System Monitor.
There, you will find each processor, RAM, your operating system, its version, kernel, hard disk space, cpu load averages over the past 1, 5, and 15 minutes, processes running, cpu, memory, and network usage graphed in real time.
Under the “Processes” tab, for example, click on the heading for the “%CPU” column to sort the list with the cpu hogs at the top (if there are any).


On the command line

ps

“Process Status” used to be “the” command to see running cpu processes.

examples:

ps aux | less
ps aux | grep httpd | awk ‘{print $1}’ ## display the first column, the owner-userid of the processes
ps auxw | less
ps aux –sort -rss | less
ps aux –sort rss

The -a option tells ps to list the processes of all users on the system rather than just those of the current user.
The -u option tells ps to provide detailed information about each process.
The -x option adds to the list processes that have no controlling terminal, such as daemons, which are programs that run in the background.
The -w option specifies Wide output.
The –sort -rss option sorts by memory usage with the highest first; “–sort rss” puts the highest last.

see the ‘man page’ for its many options / parameters


top

When your browser or wordprocessor or … whatever, “hangs” or if all you can do is move you cursor and bring up a terminal session, do it and type in “top”, type “k” and the pid of the process you see killing your cpu.

The top command provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel.
The top command monitors CPU utilization, process statistics, and memory utilization. The top section contains information related to overall system status – uptime, load average, process counts, CPU status, and utilization statistics for both memory and swap space.

By default top command displays the processes in the order of CPU usage. When the top command is running, press M (upper-case) to display processes sorted by memory usage.

top - 09:14:09 up 270 days,  3:05,  1 user,  load average: 5.65, 4.74, 4.53
Tasks: 425 total,   2 running, 420 sleeping,   0 stopped,   3 zombie
Cpu(s): 35.6% us,  8.6% sy,  0.0% ni, 51.7% id,  4.1% wa,  0.0% hi,  0.0% si
Mem:   8306036k total,  6798252k used,  1507784k free,   475028k buffers
Swap: 15626944k total,     5044k used, 15621900k free,  5283860k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND    
10938 mysql     15   0  303m 174m 5452 S 64.7  2.2   3274:29 mysqld      
26790 503       19   0 20660 7912 5900 R 10.3  0.1   0:00.31 php       
26654 684       15   0     0    0    0 Z  6.3  0.0   0:00.19 php  
26801 503       18   0 27236  14m 5748 S  5.6  0.2   0:00.17 php       
26770 634       18   0     0    0    0 Z  3.0  0.0   0:00.09 php      
23184 530       18   0  9140 5604 2060 S  2.7  0.1   0:01.89 monitor.pl     
26775 841       15   0     0    0    0 Z  2.0  0.0   0:00.06 php       
17070 nobody    16   0 31324  24m 1528 S  1.0  0.3   0:00.47 httpd   
17076 nobody    15   0 31324  25m 1672 S  0.7  0.3   0:00.40 httpd      
18108 pwsdb     16   0  3148 1260  776 R  0.7  0.0   0:01.80 top        
23747 nobody    15   0 31324  24m 1144 S  0.7  0.3   0:00.05 httpd     
23749 nobody    15   0 31324  24m 1156 S  0.7  0.3   0:00.07 httpd          
30652 root      17   0 20360  14m 1388 S  0.7  0.2   0:51.93 lfd          
16761 nobody    15   0 31324  24m 1176 S  0.3  0.3   0:00.44 httpd        
16762 nobody    15   0 31332  24m 1180 S  0.3  0.3   0:00.43 httpd       
16771 nobody    15   0 31332  24m 1548 S  0.3  0.3   0:00.40 httpd   
14975 nobody    16  ...     

Note: you need to type “q” to exit the top command display.

the important keys to remember while running top are h for help and q for quit – though you can use the system interrupt ctrl-c to get out of almost any running program.

CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is not likely to add up to exactly 100%.

Processes marked are dead processes (so-called “zombies”) that remain because their parent has not destroyed them properly. These processes will be destroyed by init(8) when the parent process exits.

Examples

1. Kill a Task Without Exiting From Top – Press k

Once you’ve located a process that needs to be killed, press ‘k’ which will ask for the process id, and signal to send. If you have the privilege to kill that particular PID, it will get killed successfully.

2. Display Selected User

Use “top -u [user]” to display a specific user processes only in the top command output.
example: top -u greg
or
While unix top command is running, press u which will ask for username

3. Display All CPUs / 1 Core per line, toggle on/off

“Top” output by default shows 1 CPU line for all the CPUs combined together as shown above.
But, for instance, if you have a quad-core computer, Press 1 (one), and “top” will display 4 line, 1 for each core. Press 1 again to switch, toggle, it back to 1 summary line.

4. Highlight Running Processes – toggle z or b

Press z or b, to toggle highlighting all running process; z=color; b=bold;   or b and z for both color and bold.
Press x (after z or b) to toggle highlighting of the current sort column (if highlighting or color, b or z, is on).
Press Z to change the color of the highlighting.

5. Change Number of Processes Displayed

By default top displays total number process based on the window height.
Press n in the Interactive mode, which prompts for a number and shows only that number of processes.
If you like to see additional process you might want to eliminate some of the top header information.

5b. show/hide heading lines displayed

* Press l – to hide / show the load average. 1st header line.
* Press t – to hide / show the CPU states. 2nd and 3rd header line.
* Press m – to hide / show the memory information. 4th and 5th line.

6. Sort by a selected column

Press O (capital o),
then
press K (cap. k) to sort by CPU usage
or press N (cap. n) to sort by memory use
or …
then, press Enter.

Or, given the default sort is by CPU, you can use the > (arrow key) to switch to sorting by the next, %MEMory column. You can navigate across and back and forth with the < and > keys.

7. Save Configuration Settings – Press W

If you’ve made any interactive changes … like these suggested in the above examples, you might want to save those as your default for the next time you call the top command. Once you’ve saved the top configuration, next time when you invoke the top command all your saved top configuration options will be used automatically.

To save the top configuration, press W, which will write the configuration files to ~/.toprc. This will display the write confirmation message as shown below.

Wrote configuration to ‘/home/[user]/.toprc’

Comments are closed.

We try to post all comments within 1 business day