How To Use The Linux Top Command To Show Running Processes

A real-time system resource monitor helps alert you to system bottlenecks

The Linux top command shows the running processes within your Linux environment that consume the most system resources.

How to Run the 'top' Command

top command

In its basic form all you need to do to show the current processes is type the following in a Linux terminal:

top

Command Output

The top command runs in the foreground and continuously updates itself. Its results show on five lines, plus a main table.

Line 1

The first line offers some basic high-level info about the system:

  • The time
  • How long the computer has been running
  • Number of users
  • Load average

The load average shows the system load time for the last 1, 5 and 15 minutes.

Line 2

The second line summarizes the number of ongoing, concurrent tasks:

Line 3

The third line summarizes CPU performance:

Line 4

The fourth line emphasizes memory:

Line 5

The fifth line highlights available swap space and total memory inclusive of swap:

  • Total swap available
  • Total swap free
  • Total swap used
  • Available memory

Main Table

The main table lists running processes:

  • Process ID
  • User
  • Priority
  • Nice level
  • Virtual memory used by process
  • Resident memory used by a process
  • Shareable memory
  • CPU used by process as a percentage
  • Memory used by process as a percentage
  • Time process has been running
  • Command
Screenshot of the Top command in Ubuntu
Gary Newell

Key Switches For the 'top' Command

Although you invoke top just by typing the name in a shell session, a few switches modify the utility's behavior:

  • -h: Show the current version
  • -c: This toggles the command column between showing command and program name
  • -d: Specify the delay time between refreshing the screen
  • -o: Sorts by the named field
  • -p: Only show processes with specified process IDs
  • -u: Show only processes by the specified user
  • -i: Do not show idle tasks

Example Usage with Switches

Let's explore several ways of using top with various switches.

Specify a Delay Time Between Screen Refreshes

To specify a delay between the screen refreshes whilst using top type the following:

top -d n

To refresh every 5 seconds type top -d 5

Obtain A List Of Columns To Sort By

To get a list of the columns with which you can sort the top command by type the following:

top -O

There are a lot of columns so you might wish to pipe the output to less as follows:

top -O | less

Sort the Columns by a Column Name

Use the previous section to find a column to sort by and then use the following syntax to sort by that column:

top -o

To sort by %CPU type the following:

top -o %CPU

Only Show the Processes for a Specific User

To show only the processes that a specific user is running use the following syntax:

top -u

For example to show all the processes that the user gary is running type the following:

top -u gary

Hide Idle Tasks

The default top view can seem cluttered and if you want to see only active processes (i.e those that are not idle) then you can ran the top command using the following command:

top -i

Adding Extra Columns to the Display

Whilst running top you can press the 'F' key which shows the list of fields that can be displayed in the table:

Use the arrow keys to move up and down the list of fields.

To set a field so that it is displayed on the screen press the 'D' key. To remove the field press "D" on it again. An asterisk (*) will appear next to displayed fields.

You can set the field to sort the table by simply by pressing the "S" key on the field you wish to sort by.

Press the enter key to commit your changes and press "Q" to quit.

Toggling Modes

Whilst running top press the A key to toggle between the standard display and an alternate display.

Changing Colors

Press the Z key to change the colors of the values within top.

There are three stages required to change the colors:

  1. Press either S for summary data, M for messages, H for column headings or T for task information to target that area for a color change
  2. Choose a color for that target, 0 for black, 1 for red, 2 for green, 3 for yellow, 4 for blue, 5 for magenta, 6 for cyan and 7 for white
  3. Enter to commit.

Press the B key to make text bold.

Change The Display Whilst Running Top

While the command runs in the foreground, toggle many features on and off by pressing relevant keys.

The following table shows the key to press and the function it provides:

Function Key Description
A Alternative display (default off)
d Refresh screen after specified delay in seconds (default 1.5 seconds)
H Threads mode (default off), summarises tasks
p PID Monitoring (default off), show all processes
B Bold enable (default on), values are shown in bold text
l Display load average (default on)
t Determines how tasks are displayed (default 1+1)
m Determines how memory usage is displayed (default 2 lines)
1 Single cpu (default off) - i.e. shows for multiple CPUs
J Align numbers to the right (default on)
j Align text to the right (default off)
R Reverse sort (default on) - Highest processes to lowest processes
S Cumulative time (default off)
u User filter (default off) show euid only
U User filter (default off) show any uid
V Forest view (default on) show as branches
x Column highlight (default off)
z Color or mono (default on) show colors
Function Keys

Alternative: 'htop' Utility

htop command

It's not installed on most distributions by default, but the htop utility functions similarly to top but it streamlines the display and ties behavior to realtime function key presses rather than a complex menu of letter-and-switch behavior. Significantly, htop shows all running processes whereas top focuses on the top processes that consume the most system resources.