1 简介
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器。top显示系统当前的进程和其他状况,是一个动态显示过程,可以自动或者通过用户按键来不断刷新当前状态。如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.。比较准确的说,top命令提供了实时的对系统处理器的状态监控,显示系统中CPU最“敏感”的任务列表。top命令可以按CPU使用、内存使用和执行时间对任务进行排序。而且该命令的很多特性都可以通过交互式命令或者在个人定制文件中进行设定.。
2 top命令的使用
下面详细介绍top命令的使用方法。
对于一个新手来说,如果从来没有使用过top命令,在命令行模式下直接输入top,可以查看到当前所有进程的信息。
[root@localhost ~]# top
显示结果中各项指标的含义,参考Linux中top命令输出指标详解。
然而,大多时候,我们需要按需筛选出最有效的信息。如何通过top命令指定参数来获取自己想要的信息呢?
- 查看top命令的可用参数
[root@localhost ~]# top -h
procps-ng version 3.3.10
Usage:
top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
通过top的帮助命令,可以指定很多参数。这些参数都是什么意思呢?
- 通过man命令查看top命令的用法
[root@localhost ~]# 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 information 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 persis‐
tent across restarts.
The program provides a limited interactive interface for process
manipulation as well as a much more extensive interface for per‐
sonal configuration -- encompassing every aspect of its opera‐
tion. 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
Manual page top(1) line 1 (press h for help or q to quit)
这就是top命令帮助文档的首页,包括top命令的作用,参数,描述,和帮助文档目录。
通过帮助文档的目录,可以看到,第一部分是COMMAND-LINE Options(命令行选项),具体内容就是讲解top命令各个参数选项的功能和用法。持续按下键盘的【向下箭头】,来到帮助文档的第一部分,下面对命令行参数选项的内容进行讲解。man帮助文档原文为英文,会在文章结尾附上。
- top命令启动参数选项说明
top -hv|-bcHiOSs -d secs -n max -u|U user -p pid -o fld -w [cols]
参数选项名称 | 含义 |
---|---|
h | 帮助 |
v | 版本 |
b | 批处理模式。可以将结果输出到其他程序或者文件。在该模式下,top不接收交互命令,直到top命令被停止,或者执行次数达到-n选项指定的值 |
c | 显示整个命令行而不只是显示命令名 |
d | 指定每两次屏幕信息刷新之间的时间间隔(单位为秒)。当然用户可以使用d或者s交互命令来改变之。 |
H | 显示所有线程的运行状态指标。如果没有该参数,会显示一个进程中所有线程的总和。在运行过程中,可以通过H命令进行交互控制。 |
i | 使top不显示任何闲置或者僵死进程。 |
n | top命令执行的最大次数 |
o | 指定排序的字段。+fieldName,按照filedName字段降序排序;-fieldName,按照filedName字段升序排序 |
O | 是o参数的辅助,打印所有可以获取到的字段名称,每行一个。然后退出top执行 |
p | 通过指定监控进程ID来仅仅监控某个进程的状态。可以指定多个,-pN1 -pN2 ... 或者 -pN1,N2,N3 ... |
q | 该选项将使top没有任何延迟的进行刷新。如果调用程序有超级用户权限,那么top将以尽可能高的优先级运行。 |
s | 使top命令在安全模式中运行。这将去除交互命令所带来的潜在危险。 |
S | 显示每个进程累计占用CPU时间模式。当指定该参数,每个进程的CPU时间会包含该进程和该进程已经死掉的子进程的时间。可以在top运行窗口通过S交互命令进行启用和关闭。 |
u/U | -u userName/userId,匹配【当前活跃用户(effective)】的进程指标信息;-U userName/userId,匹配【所有用户】,包括real, effective, saved, or filesystem |
w | -w[number] 指定显示宽度。官方建议不要使用该参数,输出宽度会和terminal自动适配。 |
注意 ** p选项和u/U选项互斥,不能同时指定 **
- top命令运行中交互命令
top命令执行过程中,可以使用的一些交互命令。从使用角度来看,熟练的掌握这些命令比掌握选项还重要一些。这些命令都是单字母的,使用方便。如果在命令行选项中使用了【s】选项,则可能其中一些命令会被屏蔽掉。
参数选项名称 | 含义 |
---|---|
Ctrl+L | 擦除并且重写屏幕。 |
h或者? | 显示帮助画面,给出一些简短的命令总结说明。 |
k | 终止一个进程。系统将提示用户输入需要终止的进程PID,以及需要发送给该进程什么样的信号。一般的终止进程可以使用15信号;如果不能正常结束那就使用信号9强制结束该进程。默认值是信号15。在安全模式中此命令被屏蔽。 |
i | 忽略闲置和僵死进程。这是一个开关式命令。 |
q | 退出程序。 |
r | 重新安排一个进程的优先级别。系统提示用户输入需要改变的进程PID以及需要设置的进程优先级值。输入一个正值将使优先级降低,反之则可以使该进程拥有更高的优先权。默认值是10。 |
S | 切换到累计模式。 |
s | 改变两次刷新之间的延迟时间。系统将提示用户输入新的时间,单位为s。如果有小数,就换算成m s。输入0值则系统将不断刷新,默认值是5 s。需要注意的是如果设置太小的时间,很可能会引起不断刷新,从而根本来不及看清显示的情况,而且系统负载也会大大增加。 |
f或者F | 从当前显示中添加或者删除项目。 |
o或者O | 改变显示项目的顺序。 |
H | 在【显示所有线程的运行状态指标】和【显示一个进程中所有线程的总和】之间进行切换 |
l | 切换显示平均负载和启动时间信息。 |
m | 切换显示内存信息。 |
t | 切换显示进程和CPU状态信息。 |
c | 切换显示命令名称和完整命令行。 |
M | 根据驻留内存大小进行排序。 |
P | 根据CPU使用百分比大小进行排序。 |
T | 根据时间/累计时间进行排序。 |
W | 将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法。 |
3 示例
top命令 | 解释 |
---|---|
top | 每隔3秒显式所有进程的资源占用情况 |
top -d 10 | 每隔10秒显式所有进程的资源占用情况 |
top -c | 每隔3秒显式进程的资源占用情况,并显示进程的命令行参数(默认只有进程名) |
top -p 1111 -p 2222 | 每隔3秒显示pid是1111 和2222的两个进程的资源占用情况 |
top -d 5 -c -p 3333 | 每隔5秒显示pid是3333 的进程的资源使用情况,并显式该进程启动的命令行参数 |
参考:
1 linux的top命令参数详解
2 top启动选项的英文原文解释
- 可以通过下面命令查看
man top
- top启动选项的英文原文解释
top -hv|-bcHiOSs -d secs -n max -u|U user -p pid -o fld -w [cols]
The typically mandatory switch ('-') and even whitespace are completely optional.
-h | -v :Help/Version
Show library version and the usage prompt, then quit.
-b :Batch-mode operation
Starts top in Batch mode, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept
input and runs until the iterations limit you've set with the `-n' command-line option or until killed.
-c :Command-line/Program-name toggle
Starts top with the last remembered `c' state reversed. Thus, if top was displaying command lines, now that field will show program names,
and visa versa. See the `c' interactive command for additional information.
-d :Delay-time interval as: -d ss.t (secs.tenths)
Specifies the delay between screen updates, and overrides the corresponding value in one's personal configuration file or the startup default.
Later this can be changed with the `d' or `s' interactive commands.
Fractional seconds are honored, but a negative number is not allowed. In all cases, however, such changes are prohibited if top is running in
Secure mode, except for root (unless the `s' command-line option was used). For additional information on Secure mode see topic 6a. SYSTEM
Configuration File.
-H :Threads-mode operation
Instructs top to display individual threads. Without this command-line option a summation of all threads in each process is shown. Later
this can be changed with the `H' interactive command.
-i :Idle-process toggle
Starts top with the last remembered `i' state reversed. When this toggle is Off, tasks that have not used any CPU since the last update will
not be displayed. For additional information regarding this toggle see topic 4c. TASK AREA Commands, SIZE.
-n :Number-of-iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top should produce before ending.
-o :Override-sort-field as: -o fieldname
Specifies the name of the field on which tasks will be sorted, independent of what is reflected in the configuration file. You can prepend a
`+' or `-' to the field name to also override the sort direction. A leading `+' will force sorting high to low, whereas a `-' will ensure a
low to high ordering.
This option exists primarily to support automated/scripted batch mode operation.
-O :Output-field-names
This option acts as a form of help for the above -o option. It will cause top to print each of the available field names on a separate line,
then quit. Such names are subject to nls translation.
-p :Monitor-PIDs mode as: -pN1 -pN2 ... or -pN1,N2,N3 ...
Monitor only processes with specified process IDs. This option can be given up to 20 times, or you can provide a comma delimited list with up
to 20 pids. Co-mingling both approaches is permitted.
A pid value of zero will be treated as the process id of the top program itself once it is running.
This is a command-line option only and should you wish to return to normal operation, it is not necessary to quit and restart top -- just
issue any of these interactive commands: `=', `u' or `U'.
The `p', `u' and `U' command-line options are mutually exclusive.
-s :Secure-mode operation
Starts top with secure mode forced, even for root. This mode is far better controlled through the system configuration file (see topic 6.
FILES).
-S :Cumulative-time toggle
Starts top with the last remembered `S' state reversed. When Cumulative time mode is On, each process is listed with the cpu time that it and
its dead children have used. See the `S' interactive command for additional information regarding this mode.
-u | -U :User-filter-mode as: -u | -U number or name
Display only processes with a user id or user name matching that given. The `-u' option matches on effective user whereas the `-U' option
matches on any user (real, effective, saved, or filesystem).
Prepending an exclamation point ('!') to the user id or name instructs top to display only processes with users not matching the one provided.
The `p', `u' and `U' command-line options are mutually exclusive.
-w :Output-width-override as: -w [ number ]
In Batch mode, when used without an argument top will format output using the COLUMNS= and LINES= environment variables, if set. Otherwise,
width will be fixed at the maximum 512 columns. With an argument, output width can be decreased or increased (up to 512) but the number of
rows is considered unlimited.
In normal display mode, when used without an argument top will attempt to format output using the COLUMNS= and LINES= environment variables,
if set. With an argument, output width can only be decreased, not increased. Whether using environment variables or an argument with -w,
when not in Batch mode actual terminal dimensions can never be exceeded.
Note: Without the use of this command-line option, output width is always based on the terminal at which top was invoked whether or not in
Batch mode.