15 Practical examples of top command

Linux top command is more powerful and useful for a system administrator to analyze the system’s process and resource status and utilization. In this tutorial, you will learn uses of top command by discussing on various top command examples.

The syntax of the top command is straightforward, and you can use like another command.

You can found the more on top command to use in our Linux command top help and example guide.

Here, you can find the most useful and daily use of top command examples in the system administrator’s life.

Linux top command Examples:

Example 1: Display all running process’s current status

The top command is use to display all information about the process like tasks, memory, CPU, and swap after executing top command use ‘q’ to quit from the top window.

top - 13:01:13 up 222 days, 21:52,  2 users,  load average: 8.73, 3.84, 4.40
Tasks: 246 total,   3 running, 243 sleeping,   0 stopped,   0 zombie
%Cpu(s): 24.8 us,  0.4 sy,  0.0 ni, 74.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6897628 free, 16915208 used,  7069600 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13168068 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S 180.4 53.5 169113:08 mysqld
31314 root      20   0  400364  93992  22156 R  20.6  0.3   0:11.80 php
31974 root      20   0  351424  44672  22064 S   0.7  0.1   0:00.12 php
19720 zabbix    20   0  105372   1436   1324 S   0.3  0.0  18:58.09 zabbix_agentd
32016 root      20   0   40924   3996   3048 R   0.3  0.0   0:00.12 top
    1 root      20   0  185224   5404   3748 S   0.0  0.0   5:55.88 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:18.96 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0  46:22.39 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 R   0.0  0.0 242:59.11 rcu_sched
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
    9 root      rt   0       0      0      0 S   0.0  0.0   4:43.91 migration/0
   10 root      rt   0       0      0      0 S   0.0  0.0   1:04.14 watchdog/0
   11 root      rt   0       0      0      0 S   0.0  0.0   1:06.35 watchdog/1

Example 2: Shorting output using “O” option (Uppercase letter ‘O’)

You can sort the top command output using any filed. To sort the output first need to press ‘Shift+O’ to get the sort field letter as shown below:

top - 13:06:03 up 222 days, 21:57,  2 users,  load average: 1.22, 2.75, 3.79
Tasks: 247 total,   3 running, 244 sleeping,   0 stopped,   0 zombie
%Cpu(s): 29.5 us, 13.7 sy,  0.0 ni, 56.7 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem : 30882436 total,  6938176 free, 16864404 used,  7079856 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13208904 avail Mem
add filter #1 (case sensitive) as: [!]FLD?VAL
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S  54.2 53.5 169116:50 mysqld
 1769 root      20   0  350340  43552  21772 R  16.9  0.1   0:00.51 php
 1919 root      20   0  346752  40052  22048 S   8.0  0.1   0:00.24 php
  517 root      20   0   52060  18404  14080 S   3.0  0.1 950:01.17 systemd-journal

Now you can press any field key-value like ‘a’ letter is use to sort process with PID (Process ID) filed.

top - 13:06:41 up 222 days, 21:58,  2 users,  load average: 0.81, 2.46, 3.65
Tasks: 237 total,   1 running, 236 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6990468 free, 16814368 used,  7077600 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13261248 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0  185224   5404   3748 S   0.0  0.0   5:55.88 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:18.96 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0  46:22.48 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S   0.0  0.0 242:59.39 rcu_sched
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
    9 root      rt   0       0      0      0 S   0.0  0.0   4:43.91 migration/0
   10 root      rt   0       0      0      0 S   0.0  0.0   1:04.15 watchdog/0
   11 root      rt   0       0      0      0 S   0.0  0.0   1:06.35 watchdog/1
   12 root      rt   0       0      0      0 S   0.0  0.0   4:11.89 migration/1
   13 root      20   0       0      0      0 S   0.0  0.0  83:50.23 ksoftirqd/1
   15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H
   16 root      rt   0       0      0      0 S   0.0  0.0   1:03.11 watchdog/2

Example 3: Show a specific user’s process

You can execute the top command with ‘-u’ option to display the all running process for the specific user.

# top -u mysql

top - 13:08:22 up 222 days, 22:00,  2 users,  load average: 0.48, 1.86, 3.31
Tasks: 238 total,   1 running, 237 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6988952 free, 16815644 used,  7077840 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13259868 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S   0.0 53.5 169117:28 mysqld

Example 4: Highlight running process in top output

You can display top command output in color, which helps you to identify the running process easily and to do this use the ‘z’ option while running the top console.

Example 5: Display the absolute path of a process

You can use the “c” option to display the absolute path of the running process while running the top command.

top - 13:12:31 up 222 days, 22:04,  2 users,  load average: 6.81, 3.08, 3.34
Tasks: 241 total,   1 running, 240 sleeping,   0 stopped,   0 zombie
%Cpu(s): 12.8 us,  0.5 sy,  0.0 ni, 86.6 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem : 30882436 total,  6907084 free, 16890004 used,  7085348 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13185228 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S  92.0 53.5 169120:35 /usr/sbin/mysqld
 3659 root      20   0  479408  98068  22904 S  13.0  0.3   0:22.61 php /var/www/manajemenkinerja/autoresponders.php
  874 www-data  20   0  470772  32888  13592 S   0.7  0.1   0:00.06 /usr/sbin/apache2 -k start
  438 www-data  20   0  546792  40212  20684 S   0.3  0.1   0:00.06 /usr/sbin/apache2 -k start
 1533 www-data  20   0  546708  39592  20124 S   0.3  0.1   0:00.06 /usr/sbin/apache2 -k start
 3270 root      20   0   40924   4064   3096 R   0.3  0.0   0:00.64 top

Example 6: Change or set screen refresh time while running top command

You can change the screen refresh time interval from default 3.0 seconds to any other value. To change the value press ‘d’ option while running top command and enter the new value, as shown below:

top - 13:13:57 up 222 days, 22:05,  2 users,  load average: 2.52, 2.60, 3.14
Tasks: 243 total,   1 running, 242 sleeping,   0 stopped,   0 zombie
%Cpu(s): 12.2 us,  0.6 sy,  0.0 ni, 87.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6917632 free, 16888536 used,  7076268 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13187424 avail Mem
Change delay from 3.0 to
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S  78.1 53.5 169122:03 mysqld
 3659 root      20   0  479408  98068  22904 S  22.9  0.3   0:36.80 php
19713 zabbix    20   0  105372   2476   2276 S   0.5  0.0  72:38.55 zabbix_agentd

Example 7: Kill running process in top

While running the top command, you can kill any process. To kill the process first find the process id and press ‘k’ key in top running console and enter the process id, as shown below:

top - 13:15:13 up 222 days, 22:06,  2 users,  load average: 1.21, 2.18, 2.95
Tasks: 241 total,   2 running, 239 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.7 us,  1.9 sy,  0.0 ni, 89.3 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6967452 free, 16837776 used,  7077208 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13237332 avail Mem
PID to signal/kill [default pid = 29560]
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S  44.2 53.5 169122:37 mysqld
  517 root      20   0   60208  24100  19776 S   0.9  0.1 950:04.34 systemd-journal
 5984 root      20   0  351424  44652  22044 S   0.4  0.1   0:00.32 php
25998 root      20   0       0      0      0 S   0.2  0.0   0:31.03 kworker/2:0

Example 8: Short top output as per CPU utilization

If you want to sort the process output in top command as per CPU utilization, you should press Shift+P key to sort processes as per the CPU usage, as shown below:

top - 13:16:39 up 222 days, 22:08,  2 users,  load average: 0.41, 1.65, 2.69
Tasks: 240 total,   1 running, 239 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6990280 free, 16815912 used,  7076244 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13260196 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S   0.7 53.5 169122:51 mysqld
    1 root      20   0  185224   5404   3748 S   0.0  0.0   5:55.89 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:18.96 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0  46:22.50 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S   0.0  0.0 242:59.75 rcu_sched
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
    9 root      rt   0       0      0      0 S   0.0  0.0   4:43.91 migration/0

Example 9: Change the priority of a process in top

The change of priority of a process is called renice, you can renice the process by using ‘r’ key while running the top command, as shown below:

top - 13:17:17 up 222 days, 22:08,  2 users,  load average: 0.43, 1.51, 2.60
Tasks: 237 total,   1 running, 236 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.2 sy,  0.0 ni, 99.4 id,  0.4 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6993600 free, 16812092 used,  7076744 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13263608 avail Mem
PID to renice [default pid = 1]
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0  185224   5404   3748 S   0.0  0.0   5:55.89 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:18.96 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0  46:22.51 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S   0.0  0.0 242:59.77 rcu_sched
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh

Example 10: Save top command output in a file

You can save the output of the top command into the file for future reference by using the following command:

$ topn 1b > top-output.txt

Example 11: Use top command in a secure mode

You can start top with secure mode forced, even for root. This mode is use to get better control through the system configuration. To run the top command in secure mode use following command:

$ top -s

top - 13:20:33 up 222 days, 22:12,  2 users,  load average: 1.05, 1.10, 2.22
Tasks: 241 total,   3 running, 238 sleeping,   0 stopped,   0 zombie
%Cpu(s): 12.0 us,  0.9 sy,  0.0 ni, 87.0 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6915972 free, 16888808 used,  7077656 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13186876 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
29560 mysql     20   0 30.462g 0.015t  19220 S  78.7 53.5 169124:15 mysqld
 8296 root      20   0  401924  95372  21980 R  22.9  0.3   0:05.36 php
 8490 root      20   0   40624   3844   3148 R   0.3  0.0   0:00.01 top
19712 zabbix    20   0  105372   2492   2372 R   0.3  0.0  63:23.81 zabbix_agentd
    1 root      20   0  185224   5404   3748 S   0.0  0.0   5:55.89 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:18.96 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0  46:22.51 ksoftirqd/0

Example 12: Top command forest view

Forest view of the process is nothing; it shows the process in a parent-child hierarchy. Sometimes you need to check the process in forest view, and you can do it by using ‘v’/’V’ key while running the top command.

top - 13:21:52 up 222 days, 22:13,  2 users,  load average: 1.26, 1.16, 2.14
Tasks: 244 total,   2 running, 242 sleeping,   0 stopped,   0 zombie
%Cpu(s): 12.1 us,  0.5 sy,  0.0 ni, 87.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 30882436 total,  6919036 free, 16886860 used,  7076540 buff/cache
KiB Swap:  1003516 total,   974504 free,    29012 used. 13190192 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0  185224   5404   3748 S   0.0  0.0   5:55.89 systemd
  517 root      20   0   60208  26212  21888 S   0.0  0.1 950:05.33  `- systemd-journal
  624 root      20   0  102968    480    480 S   0.0  0.0   0:00.00  `- lvmetad
  649 root      20   0   44972    660    528 S   0.0  0.0   0:16.92  `- systemd-udevd
  986 systemd+  20   0  100324   1892   1872 S   0.0  0.0   0:29.25  `- systemd-timesyn
 1104 message+  20   0   43020   3124   2716 S   0.0  0.0   0:09.65  `- dbus-daemon
 1128 syslog    20   0  256392   5584   2136 S   0.0  0.0 195:07.71  `- rsyslogd
 1133 root      20   0  274580   2884   2464 S   0.0  0.0  46:51.59  `- accounts-daemon
 1135 root      20   0  237536  56196   3832 S   0.0  0.2 213:03.33  `- vmtoolsd
 1138 daemon    20   0   26044   1268   1268 S   0.0  0.0   0:01.04  `- atd
 1149 root      20   0  619160  16712   6912 S   0.0  0.1  15:19.74  `- snapd
 1152 root      20   0   28996   2820   2228 S   0.0  0.0   0:20.43  `- systemd-logind

Example 13: Exit top command after specific repetition

When you type top command on the console, it keeps refreshing output until you press ‘q.’ suppose you want to auto exit top command after 20 repetitions you can use below command:

$ topn 20

Example 14: Getting help in top command

If you are a little bit confuse to use the top command with an option where it supports the various number of option, you can check the purpose of all option, and it will help you to use the correct option with top command. To get this help, you can use the ‘h’ option with top.

$ top -h

Example 15: Show manual of top command

The ‘man’ command is use to check the documentation of any command, so the below command ‘man top’ will display the manual page or documentation for top command.

$ man top

TOP(1)                                              User Commands                                             TOP(1)
NAME
       top - display Linux processes
SYNOPSIS
       top -hv|-bcHiOSs -d secs -n max -u|U user -p pid -o fld -w [cols]
       The traditional switches `-' and whitespace are optional.
DESCRIPTION
       The  top program provides a dynamic real-time view of a running system.  It can display system summary infor‐
       mation as well as a list of processes or threads currently being managed by the Linux kernel.  The  types  of
       system summary information shown and the types, order and size of information displayed for processes are all
       user configurable and that configuration can be made persistent across restarts.
       The program provides a limited interactive interface for process manipulation as well as a much  more  exten‐
       sive  interface for personal configuration  --  encompassing every aspect of its operation.  And while top is
       referred to throughout this document, you are free to name the program anything you  wish.   That  new  name,
       possibly  an alias, will then be reflected on top's display and used when reading and writing a configuration
       file.
OVERVIEW
   Documentation
       The remaining Table of Contents
           1. COMMAND-LINE Options
           2. SUMMARY Display
              a. UPTIME and LOAD Averages
              b. TASK and CPU States
              c. MEMORY Usage
           3. FIELDS / Columns Display
              a. DESCRIPTIONS of Fields
              b. MANAGING Fields
           4. INTERACTIVE Commands
              a. GLOBAL Commands
              b. SUMMARY AREA Commands
              c. TASK AREA Commands
                 1. Appearance
                 2. Content
                 3. Size
                 4. Sorting
              d. COLOR Mapping

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles