Search anything:

Top Command in Linux

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article, we have explored Top Command in Linux and showed the various information we can get from this command. You should definitely know this command effectively.

Table of contents:

  1. Introduction to Top Command
  2. Display All Running Processes
  3. Sorting The Processes
  4. Highlight Running Processes
  5. Absolute path of Running Processes
  6. Kill A Running Process
  7. Check Linux CPU Cores
  8. Save Top Command Results in File
  9. Alter Screen Refresh Interval
  10. Exit Top Command After Specific Repetition
  11. Show/ Hide columns
  12. Changing Priority of a Process
  13. When to use Top Command?

Introduction to Top Command

In UNIX-like systems, the top command is pre-installed, there is no need to install any external package. Top command provides real-time information about the running processes.

Top command tendency will be to show as much information as possible but with few keypresses you can change the format and order of the information displayed according to your needs.

Display All Running Processes

  • This command will display all running processes and details like Process ID, CPU usage, memory usage, etc.
  • It will show summary dashboard first then process list.

The column headings in the process list are as follows:

  1. PID : Process ID.
  2. USER: The owner of the process.
  3. PR : Process priority.
  4. NI : The nice value of the process.
  5. VIRT : Amount of virtual memory used by the process.
  6. RES : Amount of resident memory used by the process.
  7. SHR : Amount of shared memory used by the process.
  8. S : Status of the process.
    Status of the process can be :
  • D = Disk sleep
  • I = Idle
  • R = Running
  • S = Sleeping
  • T = Stopped
  • t = Tracing stop
  • X = Dead
  • P = Parked
  • Z = Zombie
  1. %CPU : The share of CPU time used by the process since the last update.
  2. %MEM : The share of physical memory used.
  3. TIME+ : Total CPU time used by the task in hundredths of a second.
  4. COMMAND : The command name or command line (name + options).

After executing top command we are on top display and to exit from here press q.

Sorting The Processes

To sort the processes we have some options, use these key combinations to sort :

  • Press P = sort by CPU usage
  • Press N = sort the list by ProcessID
  • Press T = sort by running time
  • Press R = reverse the order
  • Press M = Sort by the %MEM column.
  • Press T = Sort by the TIME+ column.

Press 'X' to highlight the sorted column in bold.

Display All User-Specific Processes :

Displays all User specific processes information

top -u [username]

Highlight Running Processes

Pressing 'z' while the top command is running will highlight the running processes.

Absolute path of Running Processes

Pressing 'c' in running top command will display the absolute path of the running process.

Kill A Running Process

Pressing 'k' in running top command will kill the selected process.

Check Linux CPU Cores

Pressing '1' will load the information of your CPU cores.

Save Top Command Results in File

To save the running top command results output to a file

top -n 1 -b > top.txt

Alter Screen Refresh Interval

Default screen refresh interval is set to 3.0 seconds, to change it press 'd' while the top command is running.

Exit Top Command After Specific Repetition

This command will help you exit top automatically after 10 repetitions of screen refresh.

top -n 10

Show/ Hide columns

By default top displays only few columns out of many more that it can display. If you want to add or remove a particular column or change the order of columns, then press 'f '.

Changing Priority of a Process

Press'r' key while the top command is running and then enter the PID of a process whose priority you want to change, this is known as renice.

If you dont want to remember all the keyboard shortcuts, then an alternative for top command can be Htop command in Linux/ UNIX system.

When to use Top Command?

Top command is one of the most important commands in UNIX if you are using the system professionally. A developer uses top command is the following cases:

  • To see the number of CPU cores utilized by a running process (usually, a benchmark) visually. It will give you an idea like 90% of all 64 cores are being utilized.
  • To debug scaling of applications. It can happen that applications run slower on large amount of input. One common cause is that application does not use all available cores as in smaller input.
  • As an alternative to htop command.

With this article at OpenGenus, you must have the complete idea of Top Command in Linux / UNIX system.

Top Command in Linux
Share this