Command of the Day: top

May 5, 2007

Top provides information about the processes currently running sorted by their CPU usage. Like Windows Task Manager it refreshes frequently and gives basic stats on CPU and memory usage. Another similarity is that it can be used to adjust process priority, sort by other criteria (pid, etc), etc.

A sample probably says it best:

Tasks: 103 total,   2 running, 100 sleeping,   0 stopped,   1 zombie 
Cpu(s):  8.5%us,  2.0%sy,  0.8%ni, 79.6%id,  8.8%wa,  0.1%hi,  0.2%si,  0.0%st 
Mem:   1002500k total,   442988k used,   559512k free,    60144k buffers 
Swap:  2096472k total,        0k used,  2096472k free,   236868k cached    

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 
 5103 root      15   0 62556  21m 6476 S  4.0  2.2   0:15.16 Xorg 
    1 root      18   0  2912 1844  524 S  0.0  0.2   0:01.09 init 
    2 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0 
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0 
    4 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0 
    5 root      10  -5     0    0    0 S  0.0  0.0   0:00.01 events/0 
    6 root      16  -5     0    0    0 S  0.0  0.0   0:00.00 khelper 
    7 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kthread 
   30 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kblockd/0 
   31 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid 
   32 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify 
  139 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kseriod 
  163 root      16   0     0    0    0 S  0.0  0.0   0:00.00 pdflush 
  164 root      15   0     0    0    0 S  0.0  0.0   0:00.00 pdflush 
  165 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kswapd0 
  166 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 aio/0 
 1991 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd 
 1992 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khubd

It’s worth noting that to get that output I needed to run top in batch mode and redirect to a file – the command line option to do that is ‘-b’ (i.e. top -b > output.txt).

When running top in non-batched mode there are a ton of ways to filter. Some that I found helpful were:

z Toggle color/mono mode
F Select filter criteria (it brings up a screen of filter options)
h Display help
i Toggle suppression of idle processes
P Sort by CPU usage (this is the default)
T Sort by time or cumulative time.
m Toggle display of memory stats
W Write out current settings to the ~/.toprc file
k Prompted for pid to kill
r Apply renice to a process (renice can adjust the process priority)

This is just the tip of the iceberg. I had no idea how much was available with the top command.

6 Responses to “Command of the Day: top”


  1. Most of the options you’ve listed are basically diplay options or control sorting order. An actual filter you missed is ‘u’, which will prompt you for a username, after which you’ll only be shown the processes associated with that username.

    Usually, the processes you care about are the ones associated with you as a user, rather than the system running in the background, which can largely be expected to ‘just work’.

  2. makingtheswitch Says:

    Re; Michael,

    You make a good point. I am approaching this from a single-user perspective so the user filter was not as interesting to me as I read the docs. I can appreciate its value in general, however.

  3. wariat Says:

    Try the htop similar tool, but IMO more user friendly one.


  4. htop is really intresting: http://htop.sourceforge.net/ It is provided by Ubuntu repositories, too.

  5. maniacmusician Says:

    another very useful part of top that I found was pressing M. Similarly to P, which lists tasks by CPU consumption, M lists tasks by memory consumption. It’s handy.


  6. Sorry, I didn’t make myself clear: filtering on your username will hide all of the many processes owned by root, which you usually don’t care much about anyway.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: