How to use the command top (with examples)

How to use the command top (with examples)

The top command is a powerful tool that allows you to monitor the real-time information about running processes on your system. It provides a dynamic, interactive interface that displays the CPU usage, memory usage, and other details of each running process. This article will illustrate various use cases of the top command with examples.

Use case 1: Start top

Code:

top

Motivation: Starting the top command without any arguments will launch the interactive interface that displays the real-time information about running processes. This is useful when you want to monitor the system activity continuously.

Explanation: In this use case, no arguments are passed to the top command.

Example output:

top - 15:49:02 up 2 days,  3:57,  2 users,  load average: 0.52, 0.67, 0.79
Tasks: 183 total,   1 running, 182 sleeping,   0 stopped,   0 zombie
%Cpu(s):  6.7 us,  2.9 sy,  0.1 ni, 90.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.2 st
KiB Mem : 16328820 total, 14457812 free,   657348 used,  1213628 buff/cache
KiB Swap:  2097148 total,  2061456 free,    35692 used. 15549512 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0  185040   4064   2824 S   0.0  0.0   0:03.40 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0   0:01.41 ksoftirqd/0
    4 root      20   0       0      0      0 S   0.0  0.0   0:01.19 kworker/0:0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H

Use case 2: Do not show any idle or zombie processes

Code:

top -i

Motivation: By default, the top command displays all running processes, including idle and zombie processes. However, in some cases, you might want to exclude these processes from the output to focus only on actively running processes. The -i option allows you to achieve this.

Explanation: In this use case, the -i option is passed to the top command, which instructs it to not display any idle or zombie processes.

Example output:

top - 16:02:54 up 2 days,  4:10,  2 users,  load average: 0.89, 0.94, 0.92
Tasks: 182 total,   1 running, 181 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.7 us,  8.6 sy,  0.0 ni, 76.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16328820 total, 14462844 free,   654864 used,  1214112 buff/cache
KiB Swap:  2097148 total,  2061456 free,    35692 used. 15562140 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1385 root      20   0  253228   7456   5888 S   3.0  0.0   2:08.71 Xorg
 2992 user      20   0 1378500 153556  86188 S   1.0  0.9   1:03.86 gnome-shell
 2048 user      20   0 9151552 201040 110044 S   0.0  1.2   2:16.95 chrome
 3253 user      20   0 2748480 446224 104692 S   0.0  2.7   2:25.13 code

Use case 3: Show only processes owned by given user

Code:

top -u username

Motivation: Sometimes you may want to focus on the processes owned by a specific user rather than seeing all the running processes. The -u option allows you to filter the output and display only the processes owned by the given user.

Explanation: In this use case, the -u option is used along with the username argument, where username represents the desired user.

Example output:

top - 16:12:21 up 2 days,  4:19,  2 users,  load average: 0.74, 0.87, 0.90
Tasks: 144 total,   1 running, 143 sleeping,   0 stopped,   0 zombie
%Cpu(s): 20.2 us, 12.7 sy,  0.0 ni, 66.9 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
KiB Mem : 16328820 total, 14456592 free,   673612 used,  1235568 buff/cache
KiB Swap:  2097148 total,  2061456 free,    35692 used. 15526896 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1669 user      20   0 3910944 678188  57736 S   6.7  4.2   6:23.84 java
 1942 user      20   0 2769356 862216 127040 S   2.3  5.3   8:52.73 code
 2069 user      20   0 6466960 717968  77640 S   1.3  4.4  13:23.90 chrome

Use case 4: Sort processes by a field

Code:

top -o field_name

Motivation: By default, the top command sorts the processes by CPU usage. However, in some situations, you may want to sort the processes based on a different field, such as memory usage or process ID. The -o option allows you to specify the field for sorting.

Explanation: In this use case, the -o option is used along with the field_name argument, where field_name represents the desired field for sorting.

Example output:

top - 16:20:46 up 2 days,  4:28,  2 users,  load average: 1.18, 1.26, 1.22
Tasks: 135 total,   1 running, 134 sleeping,   0 stopped,   0 zombie
%Cpu(s):  9.9 us,  5.2 sy,  0.0 ni, 84.7 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
KiB Mem : 16328820 total, 14458656 free,   684400 used,  1223764 buff/cache
KiB Swap:  2097148 total,  2060896 free,    36252 used. 15519476 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  122 root      20   0       0      0      0 R   0.7  0.0   0:07.80 kworker/4:1
 2951 user      20   0 1375020 151796  84316 S   1.0  0.9   0:40.78 gnome-shell
  388 root      20   0   60696   5696   4940 S   0.0  0.0   0:02.95 systemd-journal
  439 root      20   0       0      0      0 I   0.0  0.0   0:02.13 kworker/u16:3

Use case 5: Show the individual threads of a given process

Code:

top -Hp process_id

Motivation: In some cases, you may want to analyze the individual threads of a specific process rather than seeing the overall process information. The -H option allows you to display the individual threads associated with a given process.

Explanation: In this use case, the -H option is passed to the top command along with the process_id argument, where process_id represents the ID of the process.

Example output:

top - 16:30:02 up 2 days,  4:37,  2 users,  load average: 1.27, 1.26, 1.23
Tasks: 158 total,   2 running, 156 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.2 us,  3.7 sy,  0.0 ni, 88.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.1 st
KiB Mem : 16328820 total, 14471120 free,   672932 used,  1224692 buff/cache
KiB Swap:  2097148 total,  2060896 free,    36252 used. 15530920 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  319 root      20   0       0      0      0 S   5.7  0.0   0:36.65 kworker/0:1
  922 user      20   0 7531852 643408  59048 R   2.2  4.0  18:13.84 chrome
  920 user      20   0 3537348 174956  91884 R   0.7  1.1   4:25.41 chrome
   10 root      20   0       0      0      0 I   0.3  0.0   0:52.67 rcu_gp

Use case 6: Show only the processes with the given PID(s), passed as a comma-separated list

Code:

top -p $(pgrep -d ',' process_name)

Motivation: Normally, you wouldn’t know the process IDs (PIDs) off hand. However, in some cases, you might want to monitor specific processes by their names. This use case demonstrates how to find the PIDs corresponding to a process name using pgrep and then pass them as an argument to the top command.

Explanation: In this use case, the -p option is used along with the process IDs obtained from the pgrep command. The pgrep -d ',' process_name command lists the PIDs of the processes with the given process_name and separates them by commas.

Example output:

top - 16:40:33 up 2 days,  4:48,  2 users,  load average: 0.93, 1.10, 1.17
Tasks: 176 total,   1 running, 175 sleeping,   0 stopped,   0 zombie
%Cpu(s): 17.4 us,  9.5 sy,  0.0 ni, 73.0 id,  0.0 wa,
Tags :

Related Posts

How to Use the Command "monodis" (with examples)

How to Use the Command "monodis" (with examples)

The “monodis” command is a Mono Common Intermediate Language (CIL) disassembler.

Read More
How to use the command xzdiff (with examples)

How to use the command xzdiff (with examples)

The xzdiff command is used to invoke the diff command on files that have been compressed with various compression algorithms such as xz, lzma, gzip, bzip2, lzop, or zstd.

Read More
How to use the command 'auracle' (with examples)

How to use the command 'auracle' (with examples)

This article provides examples of how to use the auracle command-line tool to interact with Arch Linux’s User Repository (AUR).

Read More