The Linux top command is used to show all the running processes within your Linux environment This guide shows you how to use the top command by explaining the different switches available and the information that is displayed:
How To Run The Top Command
In its basic form all you need to do to show the current processes is type the following in a linux terminal
What Information Is Shown:
The following information is displayed when you run the Linux top command:
- 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.
- CPU usage as a percentage by the user
- CPU usage as a percentage by system
- CPU usage as a percentage by low priority processes
- CPU usage as a percentage by idle processes
- CPU usage as a percentage by io wait
- CPU usage as a percentage by hardware interrupts
- CPU usage as a percentage by software interrupts
- CPU usage as a percentage by steal time
This guide gives a definition of what CPU usage means.
- Total swap available
- Total swap free
- Total swap used
- Available memory
This guide gives a description of swap partitions and whether you need them.
- 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
Here is a good guide discussing computer memory.
Keep Linux Top Running All The Time In The Background
You can keep the top command easily available without having to type the word top each time into your terminal window.
To pause top so that you can continue using the terminal, press CTRL and Z on the keyboard.
To bring top back to the foreground, type fg.
Key Switches For The Top Command:
- -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
Show The Current Version
Type the following to show the current version details for top:
Output is in the form procps -ng version 3.3.10
Specify A Delay Time Between Screen Refreshes
To specify a delay between the screen refreshes whilst using top type the following:
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:
There are a lot of columns so you might wish to pipe the output to less as follows:
top -O | less
Sort The Columns In The Top Command 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:
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:
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:
Adding Extra Columns To The Top 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.
Whilst running top you can press the “A” key to toggle between the standard display and an alternate display.
Press the “Z” key to change the colors of the values within top.
There are three stages required to change the colors:
- 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
- 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
- Enter to commit
Press the “B” key to make text bold.
Change The Display Whilst Running Top
Whilst the top command is running you can toggle many of the features on and off by pressing relevant keys whilst it is running.
The following table shows the key to press and the function it provides:
||Alternative display (default off)
||Refresh screen after specified delay in seconds (default 1.5 seconds)
||Threads mode (default off), summarises tasks
||PID Monitoring (default off), show all processes
||Bold enable (default on), values are shown in bold text
||Display load average (default on)
||Determines how tasks are displayed (default 1+1)
||Determines how memory usage is displayed (default 2 lines)
||Single cpu (default off) – i.e. shows for multiple CPUs
||Align numbers to the right (default on)
||Align text to the right (default off)
||Reverse sort (default on) – Highest processes to lowest processes
||Cumulative time (default off)
||User filter (default off) show euid only
||User filter (default off) show any uid
||Forest view (default on) show as branches
||Column highlight (default off)
||Color or mono (default on) show colors
There are more switches available and you can read more about them by typing the following into your terminal window: