I don’t know about others, but I often use the command top to check the system process occupation. If you find a process that occupies an exception, do something about it.

! [](https://upload-images.jianshu.io/upload_images/24762785-912d9839fa5024a4.jpg?imageMogr2/auto-orient/strip%7CimageView 2/2/w/1240)

The command that

The top command is a commonly used performance analysis tool in Linux. It displays the resource usage of each process in the system in real time, similar to the Task manager in Windows. Top is a dynamic display process, that is, the current state can be continuously refreshed by the user’s keystrokes.

If this command is executed in the foreground, it monopolizes the foreground until the user terminates the program. More specifically, the top command provides real-time monitoring of the state of the system processor.

It displays a list of the most CPU sensitive tasks on the system. This command can sort tasks by CPU usage, memory usage, and execution time. And many of the features of the command can be set through interactive commands or in personal customization files.

The command format

Parameters of top –

Command function

This section describes how to view information about processes that are running, including process IDS, memory usage, and CPU usage

The command parameter

The following are the parameters that can be followed after the top command. Generally, -u is used.

-b indicates batch processing

-c displays complete governance commands

-I is to ignore the failure process

-s indicates the confidential mode

-s is the cumulative mode

-i is < time > Set interval

-u is < User name > Specifies the user name

-p is < process number > specifies the process

N is the number of times that the < times > loop displays

Command shows

! [](https://upload-images.jianshu.io/upload_images/24762785-e785a29174e6d635.jpg?imageMogr2/auto-orient/strip%7CimageView 2/2/w/1240)

top

Enter top to dynamically display all process information. Then, press Shift +> or Shift +< to switch the sorting to CPU usage by default.

[root@210789 ~]# top

Top-23:41:42 UP 25 days, 10:55, 1 User, Load Average: 0.00, 0.00, 0.00

Tasks: 118 total, 2 running, 116 sleeping, 0 stopped, 0 zombie

%Cpu(s): 0.2us, 0.2SY, 0.0Ni, 99.7id, 0.0wa, 0.0hi, 0.0Si, 0.0st

MiB Mem: 3635.0 Total, 295.2 Free, 1605.4 Used, 1734.4 Buff/Cache

MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1740.3 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

336386 memcach+ 20 0 442880 34300 1736 S 0.3 0.9 12:10.84 memcached

380953 root 10-10 166012 33784 16232 S 0.3 0.9 77:41.24 AliYunDun

1 root 20 0 179172 11116 8168 S 0.0 0.3 0:20.15 systemd

2 root 20 00 0 S 0.0 0.0 0:00.44 kthreadd

3 root 0-20 00 0 I 0.0 0.0 0:00.00 rcu_gp

4 root 0-20 00 0 I 0.0 0.0 0:00.00 rcu_par_gp

6 root 0-20 00 0 I 0.0 0.0 00.00 kworker/0:0 h-kblockd

8 root 0-20 00 0 I 0.0 0.0 00.00 mm_percpu_wq

9 root 20 0 0 0 S 0.0 0.0 0:02.06 ksoftirqd/0

10 root 20 0 0 0 0 I 0.0 0.0 2:42.80 rcu_sched

11 Root rt 00 0 S 0.0 0.0 0:00.10 Migration /0

12 root rt 00 0 S 0.0 0.0 0:00.02 watchdog/0

13 root 20 00 0 S 0.0 0.0 00.00 CPUHP /0

14 root 20 00 0 S 0.0 0.0 0:00.00 CPUHP /1

15 root rt 0 0 0 S 0.0 0.0 0:01.81 watchdog/1

16 Root rt 00 0 S 0.0 0.0 0:00.10 Migration /1

17 root 20 0 0 0 S 0.0 0.0 0:01.80 ksoftirqd/1

19 root 0-20 0 0 0 I 0.0 0.0 0:00. 00 kworker/H – kblockd 1:0

21 root 20 00 0 S 0.0 0.0 00.00 kdevtmpfs

22 root 0-20 00 0 I 0.0 0.0 0:00.00 netns

23 root 20 00 0 S 0.0 0.0 0:00.10 kauditd

26 Root 20 00 0 S 0.0 0.0 0:00.98 khungtaskd

27 root 20 00 0 S 0.0 0.0 00.00 oom_reaper

28 root 0-20 00 0 I 0.0 0.0 0:00.00 writeback

29 root 20 00 0 S 0.0 0.0 00.00 kcompactd0

30 root 25 5 00 0 S 0.0 0.0 0:00.00 KSMD

31 root 39 19 0 0 0 S 0.0 0.0 0:04.55 khugepaged

32 root 0-20 00 0 I 0.0 0.0 0:00.00 crypto

33 root 0-20 00 0 I 0.0 0.0 00.00

34 root 0-20 00 0 I 0.0 0.0 00.00 kblockd

35 root 0-20 00 0 I 0.0 0.0 00.00

36 root 0-20 00 0 I 0.0 0.0 0:00.00 md

37 root 0-20 00 0 I 0.0 0.0 0:00.00 edac-poller

38 root rt 00 0 S 0.0 0.0 0:00.00 watchdogd

60 root 20 00 0 S 0.0 0.0 0:00.36 kswapd0

153 root 0-20 00 0 I 0.0 0.0 0:00.00 kthrotld

154 root 0-20 00 0 I 0.0 0.0 0:00.00 acpi_thermal_pm

155 root 0-20 00 0 I 0.0 0.0 00.00 kmpath_rdacd

156 root 0-20 00 0 I 0.0 0.0 0:00.00

158 root 0-20 00 0 I 0.0 0.0 00.00 ipv6_addrconf

159 root 0-20 00 0 I 0.0 0.0 0:00.00 KSTRP

413 root 0-20 0 0 0 I 0.0 0.0 0 0 02.68 kworker/1:1 h-xfs-log /vda1

428 root 0-20 00 0 I 0.0 0.0 0:00.00 ata_sff

! [](https://upload-images.jianshu.io/upload_images/24762785-654a73998002b4c2.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

top -u

Enter top -u user, for example, top -u apache to display information about the Apache process. Run the top-u mysql command to display the database process information.

[root@210789 ~]# top -u apache

Top-23:43:34 Up 25 days, 10:57, 1 User, Load Average: 0.08, 0.05, 0.01

Tasks: 118 total, 1 running, 117 sleeping, 0 stopped, 0 zombie

%Cpu(s): 0.3us, 0.2SY, 0.0Ni, 99.3ID, 0.0wa, 0.0hi, 0.2Si, 0.0st

MiB Mem : 3635.0 total, 291.6 free, 1608.8 used, 1734.6 buff/cache

MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1736.8 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

430740 Apache 20 0 2312532 305912 49332 S 0.0 8.2 0:23.45 HTTPD

430741 Apache 20 0 2305464 306720 49784 S 0.0 8.2 0:26.47 HTTPD

430742 Apache 20 0 2297356 290864 48532 S 0.0 7.8 0:22.91 HTTPD

430940 Apache 20 0 2252668 251372 47936 S 0.0 6.8 0:14.52 HTTPD

432551 Apache 20 0 2175152 137612 45244 S 0.0 3.7 0:02.32 HTTPD

[root@210789 ~]# top -u mysql

Top-23:44:11 Up 25 days, 10:58, 1 User, Load Average: 0.04, 0.04, 0.01

Tasks: 117 total, 1 running, 115 sleeping, 1 stopped, 0 zombie

%Cpu(s): 0.8us, 1.0SY, 0.0Ni, 98.2ID, 0.0wa, 0.0hi, 0.0Si, 0.0st

MiB Mem : 3635.0 total, 290.4 free, 1609.8 used, 1734.7 buff/cache

MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1735.8 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

336344 mysql 20 0 2552992 309912 25192 S 0.0 8.3 10:33.91 mariadbd

Subsequent instructions

These commands are generally sufficient, but if you need more, you can change the parameters and check for yourself. In addition to the shift+< and shift+> commands mentioned in this article, you can also use the following commands in the top command window for interactive operations. In other words, under the top command, press the following key to perform corresponding operations.

! [](https://upload-images.jianshu.io/upload_images/24762785-099987a6bfc06c89.jpg?imageMogr2/auto-orient/strip%7CimageView 2/2/w/1240)

H: Display the help screen and give a brief summary of the command

K: Terminate a process

I: Ignore idle and dead processes, this is an on-off command

Q: Exit the program

R: Reprioritizes a process

S: Switch to cumulative mode

S: Change the delay between refreshes (in s), if there is a decimal, to ms. Enter a value of 0 and the system continuously refreshes. The default value is 5s

F or f: Adds or removes items from the current display

O or O: Changes the order in which items are displayed

L: Switch to display load average and startup time

M: Displays memory information

T: Displays the process and CPU status

C: Switch to display the command name and complete command line

M: Sort by the resident memory size

P: Sorts the CPU usage by percentage

T: Sort by time/accumulated time

W: Writes the current Settings to the ~/.toprc file