Home

 › 

Articles

 › 

Understanding the top Command in Linux, with Examples

LINUX - word on a white sheet against the background of the laptop keyboard. Internet concept

Understanding the top Command in Linux, with Examples

Key Points

  • Unix-like operating systems use commands, like the top command in Linux, to access many functions to which Windows or macOS would assign a graphical user interface.
  • Learning the commands associated with Unix-like operating systems is crucial for maximizing your experience.
  • The top command in Linux is used to open an interactive process monitoring window that displays usage statistics and a list of running processes.
  • The top command can be customized with additional commands to perform various tasks, such as closing the process monitor, sending output to a file, and sorting processes by user.

Unix-like operating systems use commands, like the top command in Linux, to access many functions to which Windows or macOS would assign a graphical user interface. Thus, users of these operating systems are heavily encouraged to learn the commands associated with their operating system of choice. 

Not learning these commands will severely hinder your ability to get the most out of your experience. Unix-like operating systems focus a lot less on providing a graphical user interface. They’re primarily designed for programmers and people experienced in code environments. Let’s assess the top command in Linux, what it is, and how to use it.

What Is the top Command in Linux?

You can use the “top” command in Linux to open an interactive process monitoring window, pictured below. The upper half of the window shows the usage statistics, while the lower shows a list of processes currently running.

top command
The top command in Linux outputs a window that monitors running processes on your computer.

The upper half of the top command window has five lines that display essential information about your computer’s performance. They are as follows:

LabelData
topSystem uptime
TasksSystem processes
%CPUProcessor values
MiB MemPhysical memory usage
MiB SwapVirtual memory usage

The process monitor has several columns containing essential data about your system’s processes and performance. They are as follows:

LabelData
PIDThe unique Process ID
PRThe process priority. A lower number in this column indicates higher priority in the system.
VIRTThe total virtual memory the process is currently using, updated in real-time.
USERThe name of the user running the process
%CPUThe total percentage of the CPU’s power the process is currently using, updated in real-time
TIME+The CPU time represented through hundredths of a second. This is functionally the same as TIME but has more granularity since it clocks smaller increments.
SHRThe shared memory size of a process in kilobytes.
NIThe Nice Value of the process. A negative number represents a higher priority.
%MEMThe total memory usage of the task
RESThe total physical RAM usage measured in kilobytes.
COMMANDThe name of the command used to start the process

How Do I Execute the top Command?

top command
Linux commands like “top” allow you to interact with your computer.

To execute the top command, all you have to do is open the command line and enter “top” with no capitalization. It looks like this:

top

That’s it. However, you can execute additional commands with the top command to make it do extra things. Let’s examine those alternative prompts.

Close the Process Monitor

To close the process monitor, press the “q” key.

Automatically Close the Process Monitor

You can set the process monitor to close automatically after a certain number of iterations. To do that, you’ll want to enter the following into the command line:

top -n int

Enter a number for int, and the process monitor will automatically close after it refreshes that many times. If you don’t enter this, the program will continue to run and refresh until you manually close it with q.

Send the Output from top to a File

You can also send top’s output to a file of your choice using the batch command. To do that, enter the following into the command line:

top -b > output.txt

You can change the output to whatever you’d like; we just wanted it to be obvious how you were supposed to do everything.

Open the Process Monitor in Safe Mode

You can open the process monitor in safe mode using the following command:

top -s

Opening the process in safe mode will force it to use as few resources as possible and only the minimum required drivers. It will also force it to be a single-user instance rather than a multi-user instance.

Show the Processes Sorted by User

If you want to see what processes a specific user is running, you can do that by entering the following command:

top -u user

Replace “user” with the person’s username, and then the top command will show you what processes they’re running on the computer when you run it.

Show top Command Syntax

If you want to know the top command’s syntax, you can use the following command:

top -h

This will bring up the syntax for a wide variety of commands that you can execute alongside top.

Show the Delay Time Between Screen Updates

If you input the following command:

top -d seconds.tenths

It will have the process monitor display the delay between refreshes.

Open the Process Monitor in the Last Saved State

If you’ve got specific settings you want to maintain between your iterations of the process monitor, you open the program in the same state you last opened it in with the following command:

top -c

This command will maintain the state of your top command window between sessions. It will allow you to keep the settings and commands you opened alongside it functioning without entering them all individually.

Monitor Only Specific Processes

You can also monitor specific processes using the following command:

top -p PID1, PID2…

To use this command, you need to know the process PID already, but you can enter as many PIDs as you want to and monitor the usage statistics of just those processes.

Highlight Running Processes

linux
You can interact with your processes directly from the top command window.

Highlighting running processes can help you identify which ones are giving you trouble regarding usage statistics. While the process monitor is open, you can highlight running processes by pressing the “z” key. That will make any currently running programs appear in color, making them easier to find.

Kill Processes

If you want to shut a process down from the monitor window, you can highlight the program you want to kill and press the “k” key.

Sort Programs by CPU Usage

If you’re trying to figure out what’s hogging your entire CPU, you can sort the process monitor by CPU usage. Press SHIFT + P while the window is open to sort it by use.

Change Display Units

You can change the units in which your process monitor displays usage by pressing the “e” key while the window is open. You can set the display unit to kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, or exbibytes.

Toggle Usage Graphs

If you’d like to see a graphical representation of your usage statistics, you can toggle that by pressing the “t” key.

Show the Absolute Path of a Process

If you need to know where a process is running from, you can access its absolute path — the path from the root — by pressing the “c” key while the PID is highlighted.

Show Process Hierarchy

If you want to know which processes were opened or influenced by others, press the “v” key to open the process hierarchy.

Show Only Active Tasks

If you want the process monitor only to show active tasks, you can have it sort those to the top by pressing the “i” key.

Limit Process Number

If you only want to see the top few processes, you can limit the number of monitored scripts by pressing the “n” key. The top window will then prompt you to input the number of programs you want it to track. Input how many programs you want to see — such as the top 5, 10, etc. — and let it re-sort the information.

Change Process Priority

You can also change the priority the computer assigns to its running processes from the top command. Highlight the PID of the process you want to adjust and press the “r” key. The window will prompt you to enter a new Nice Value, which controls the priority.

Frequently Asked Questions

What is the command line in Linux?

The command line in Linux is the name for the text interface you can use to interact with your computer. It is sometimes referred to as a shell, terminal, or console.

What is the top command in Linux?

The top command in Linux is a command line prompt that brings up the Linux process monitor.

What is the syntax for the top command?

The top command’s syntax can be accessed by typing “top -h” into the command line.

How do I open the top command in safe mode?

To open the top command in safe mode, input “top -s” into the command line.

How do I kill a process in the top command?

To kill a process from the top command, highlight its PID and press the “k” key.

To top