日期:2014-05-16  浏览次数:20771 次

在Linux下怎样让top命令启动之后就按内存使用排序(或CPU使用排序)?

在Linux下怎样让top命令启动之后就按内存使用排序(或CPU使用排序)?

?

top 命令的参数中没有方法可以直接做到。

?

man top 写道
NAME
?????? top - display Linux tasks

SYNOPSIS
?????? top -hv | -bcHisS -d delay -n iterations -p pid [, pid ...]

?????? The traditional switches ’-’ and whitespace are optional.

?????? -b : Batch mode operation
?????? -c : Command line/Program name toggle
?????? -d : Delay time interval as:? -d ss.tt (seconds.tenths)
?????? -h : Help
?????? -H : Threads toggle
?????? -i : Idle Processes toggle
?????? -n : Number of iterations limit as:? -n number
?????? -u : Monitor by user as:? -u somebody
?????? -U : Monitor by user as:? -U somebody
?????? -p : Monitor PIDs as:? -pN1 -pN2 ...? or? -pN1, N2 [,...]
?????? -s : Secure mode operation
?????? -S : Cumulative time mode toggle
?????? -v : Version
?????? -M : Detect memory units

?

但是可以在文本全屏界面显示出来之后使用交互式命令来进行,比如 按大写M可以使进程显示按内存使用排序,按大写P按CPU使用排序。

按大写W可以将当前的设置保存到配置文件中,一般就是 ~/.toprc,这样就可以在下次启动top命令是按照设定的排序方式来显示了。

man top 写道
???????? W :Write_the_Configuration_File
????????????? This? will save all of your options and toggles plus the current
????????????? display mode and delay? time.?? By? issuing? this? command? just
????????????? before? quitting? top, you will be able restart later in exactly
????????????? that same state.

???????? 1 :Toggle_Single/Separate_Cpu_States? --? On/Off
????????????? This command affects how the ’t’ command’s Cpu States portion is
????????????? shown.? Although this toggle? exists? primarily? to? serve? mas-
????????????? sively-parallel SMP machines, it is not restricted to solely SMP
????????????? environments.

?????? SORTING of task window
???????? For? compatibility,? this? top? supports? most of the former top sort
???????? keys.? Since this is primarily a service to former top? users,? these
???????? commands do not appear on any help screen.
??????????? command?? sorted field????????????????? supported
????????????? A???????? start time (non-display)????? No
????????????? M???????? %MEM????????????????????????? Yes
????????????? N???????? PID?????????????????????????? Yes
????????????? P???????? %CPU????????????????????????? Yes
????????????? T???????? TIME+???????????????????????? Yes

?

下面是一个 ~/.toprc 文件的样本。这个文件有点复杂,没仔细看过。

RCfile for "top with windows"           # shameless braggin'
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.000, Curwin=0
Def     fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX
        winflags=62777, sortindx=13, maxtasks=0
        summclr=1, msgsclr=1, headclr=3, taskclr=1
Job     fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX
        winflags=62777, sortindx=0, maxtasks=0
        summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem     fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX
        winflags=62777, sortindx=13, maxtasks=0
        summclr=5, msgsclr=5, headclr=4, taskclr=5
Usr     fieldscur=ABDECGfhijlopqrstuvyzMKNWX
        winflags=62777, sortindx=4, maxtasks=0
        summclr=3, msgsclr=3, headclr=2, taskclr=3