服务器监控命令top的几个参数

最近打算开始陆续将服务器运维的一些经验及知识记录下来,以便提供给将来的自己更多的工作便利。初步的想法是将基础功能累加堆积成一个成型的运维工具,自己用用挺好的。

今天第一篇写的是top命令,top命令是Linux系统监控中一个常用命令,该命令有很多参数,因此博主man了一下,挑了几个常用的,记录下来,以配合将来的自用运维工具。

初步想法是利用shell脚本来进行监控,得到服务器各个部分的实时信息。之后使用http提供api接口,供手机客户端显示信息。

命令参数

Z,B Global: ‘Z’ change color mappings; ‘B’ disable/enable bold
颜色和字体加强,比较有用,看得比较清楚

l,t,m  Toggle Summaries: 'l' load avg; 't' task/cpu stats; 'm' mem info

l,t,m分别对应 的 load avg,task/cpu stats, 内存信息这三大部分是否显示在top命令中。

1,I  Toggle SMP view: '1' single/separate states; 'I' Irix/Solaris mode

1 SMP 视图,具体来看貌似显示多个逻辑CPU的信息。

I 为 Irix/Solaris模式,即一个任务的CPU利用率将以CPU的总数分开显示

f,o  Fields/Columns: 'f' add or remove; 'o' change display order

f 命令会进入选择修改需要显示的列的模式,所列的列前带*号,即会显示,否则隐藏

o 命令进行列排序

F or O  Select sort field

选择排序列

c,i,S  Toggle: 'c' cmd name/line; 'i' idle tasks; 'S' cumulative time

c命令为command列命令行或程序名称的切换

u  Show specific user only

选择一个特别的用户

n or #  Set maximum tasks displayed

设置进程数量

k,r  Manipulate tasks: 'k' kill; 'r' renice

kill进程或者提供进程优先级

d or s  Set update interval

设置top延迟时间

W  Write configuration file

写入配置文件

q  Quit

退出top

交互命令

-a : Sort by memory usage
This switch makes top to sort the processes by allocated memory

内存使用情况进行排序。

-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’ commandline
option or until killed.

Batch 模式,通常被用来将top的输出作为其他程序的输入,或者保存为文件。一般配合-n参数使用,运行时无法在console中进行交互。

-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.

Command 列显示名称切换,可以是命令行完整的运行命令及参数,也可以是程序名称

-d : Delay time interval as:  -d ss.tt (seconds.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.

每次top刷新的延迟时间,一般是秒,不能用负数。在交互模式下可以使用d或者s来更改。

-H : Threads toggle
Starts top with the last remembered ’H’ state reversed.
When this toggle is On, all individual threads will be displayed.
Otherwise, top displays a summation of all threads in a process.

可以使用shift+h 进行切换,开启时显示独立线程,否则只显示包含所有进程的单一进程。

-i : Idle Processes toggle
Starts top with the last remembered ’i’ state reversed.
When this toggle is Off, tasks that are idled or zombied will not be displayed.

可以使用shift+i 进行切换,开启时显示空闲或僵尸进程,否则不显示。

-m : VIRT/USED toggle
Reports USED (sum of process rss and swap total count) instead of VIRT

内存和交换分区的显示开关。

-M : Detect memory units
Show memory units (k/M/G) and display floating point values in the memory summary.

设置内存单位(k/M/G)

-n : Number of iterations limit as:  -n number Specifies the maximum number of iterations, or frames,
top should produce before ending.

top的运行次数,-n 后跟次数,完成该次数后自动退出top。

-p : Monitor PIDs as:  -pN1 -pN2 ...  or  -pN1, N2 [,...]
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.  Comingling both approaches is permitted.

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 the ’=’ interactive command.

使用进程ID号监控。只在参数形式下可用,如果进入之后想切换回所有进程,使用‘=’就可以。

-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 5. FILES).

安全模式,禁用了一些危险的交互命令,感觉K会被禁用吧。

-S : Cumulative time mode toggle
Starts  top  with the last remembered ’S’ state reversed.
When ’Cumulative 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.

累加模式,该模式启用时,会列出每个进程和它的子线程所使用的cpu时间。

-u : Monitor by user as:  -u somebody
Monitor only processes with an effective UID or user name matching that given.

只监控某用户的线程情况,参数somebody可以是用户名或UID

-U : Monitor by user as:  -U somebody
Monitor only processes with a UID or user name matching that given.
This matches real, effective, saved, and filesystem UIDs.

同上, 参数只能是UID