Top Command in Linux with Examples

Top Command in Linux with Examples

Top command is used to capture the snapshot of processes running on your system in real-time. It also shows the health of system that are CPU utilization, and physical and virtual memory usage.

In this article, we are showing some examples of top command in Linux. All commands have been executed on Red Hat Enterprise Linux 9.

Syntax

The general syntax of the top command is as follows:

top [OPTIONS]

Examples

List all running processes

To show all running processes on terminal, you would run:

top

The top row shows the uptime, number of users currently logged in, and average load. The second row shows the number of tasks currently running, sleeping, stopped, and zombie tasks. Third row shows the CPU utilization while the fourth and fifth rows show physical and virtual memory utilization respectively.

The top command output changes every three seconds. Press q to quit the command.

Kill a specific process

You can kill a specific process while the top command is running. Press ‘k’ from the keyboard and enter the PID of process you want to kill.

You will see the following on terminal.

Change priority of the process

You can change the priority of any process by pressing ‘r’ from the keyboard during top command output.

You will see the following on terminal.

Input the process ID you want to renice and press Enter key from the keyboard. Input a new nice value.

Exit the command after X repetitions

Suppose you want the top command to quit after 3 repetitions. You would run:

top -n 3

Change refresh time

As we have mentioned, the top command refreshes the output every 3 seconds. You can change this delay time. Press ‘d’ from the keyboard and input the new value.

Here we have input 5 seconds.

Filter the processes by user

You can filter the processes of top command by user name. Just type u and then input the username followed enter key from the keyboard.

We have filtered the processes by user name root.

Sort processes by specific column

By default, top command sort the processes by %CPU column (in descending order). However, you can sort the process by using one of the following columns.

  • M. Sort by the %MEM column.
  • N. Sort by PID column.
  • T. Sort by the TIME+ column.
  • P. Sort by the %CPU column.

Change memory display units

By default physical memory and virtual memory is shown in MegaBytes. You can change this unit in one of the following:

  • kibibytes (KiB)
  • mebibytes (MiB)
  • gibibytes (GiB)
  • tebibytes (TiB)
  • pebibytes (PiB)
  • exbibytes (EiB )

Press E to change the memory units of physical and virtual memory. Press e to change the memory units of processes as shown.

Filter processes by a column value

You can filter the processes by a column value. Suppose we want want to filter the processes on following value. Press the o key and input the condition.

%MEM>5.0

The output now looks like:

Conclusion

We have shown you some of the examples of top command and how to use it. See manual pages (man top) of this command for more help.