一天一条Linux指令-top

用途说明

top命令可以显示Linux任务情况(display Linux tasks),通常用于实时监视Linux任务情况,比如可以找出占CPU最多的进程、占内存最多的进程,查看当前系统的忙闲程度,它涵盖了uptime、free、w、ps等命令的一些功能。它以全屏幕文本方式显示了系统运行时长、当前终端数量、系统负荷、任务数、CPU占用情况、内存使用情况、各个进程的情况,还可以按CPU占用情况排序、内存使用情况排序。对于系统管理员和软件开发来说top都是一个很好的工具,特别是用于性能优化。

 

常用参数

格式:top

交互模式。只显示命令,而不是命令行。

 

格式:top -c

交互模式。显示命令行,而不是只显示命令。这对于显示java进程有些帮助。

 

格式:top -p pid

格式:top -p pid1 -p pid2

交互模式。但是只显示进程号pid对应的信息。可以指定多个pid。

 

格式:top -s

交互模式。安全模式。这将去除交互命令所带来的潜在危险。非安全模式可以kill进程、改变进程优先级等。

 

格式:top -b

批处理模式。可用于脚本处理。不断的输出任务信息,包括所有进程。

 

格式:top -b -n n

批处理模式。输出n 次。比如:top -b -n 1 只显示一次top信息。

 

交互方式常用指令

M(shift+M) 按内存占用情况排序。可以找出占CPU多的进程来。

P(shift+P)   按PCU占用情况排序。可以找出占内存多的进程来。

1 (数字1)    切换显示每个CPU的详细情况/显示CPU的总体情况。较低版本的top命令,默认把所有的CPU都显示出来了;较高版本的只显示CPU总体情况。

q                   退出。

 

使用示例

示例一

[root@web ~]# top 

top - 09:57:09 up 125 days, 13:38,  4 users,  load average: 0.13, 0.05, 0.14
Tasks: 144 total,   1 running, 138 sleeping,   0 stopped,   5 zombie
Cpu(s):  1.0%us,  0.5%sy,  0.0%ni, 97.6%id,  0.5%wa,  0.1%hi,  0.2%si,  0.0%st
Mem:   4075420k total,  3924736k used,   150684k free,   176312k buffers
Swap:  5406712k total,      112k used,  5406600k free,  2150380k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                
29115 root      15   0  2332  968  720 R  2.0  0.0   0:00.01 top                                                                    
    1 root      15   0  2072  632  540 S  0.0  0.0   0:49.30 init                                                                   
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:03.06 migration/0                                                            
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.71 ksoftirqd/0                                                            
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0                                                             
    5 root      RT  -5     0    0    0 S  0.0  0.0  15:01.52 migration/1                                                            
    6 root      34  19     0    0    0 S  0.0  0.0   0:05.64 ksoftirqd/1                                                            
    7 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/1                                                             
    8 root      10  -5     0    0    0 S  0.0  0.0   0:04.82 events/0                                                               
    9 root      10  -5     0    0    0 S  0.0  0.0  26:55.92 events/1                                                               
   10 root      11  -5     0    0    0 S  0.0  0.0   0:09.14 khelper                                                                
   11 root      10  -5     0    0    0 S  0.0  0.0   0:00.08 kthread                                                                
   15 root      10  -5     0    0    0 S  0.0  0.0   9:04.05 kblockd/0                                                              
   16 root      10  -5     0    0    0 S  0.0  0.0 134:42.49 kblockd/1                                                              
   17 root      14  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid                                                                 
  145 root      14  -5     0    0    0 S  0.0  0.0   0:00.00 cqueue/0                                                               
  146 root      14  -5     0    0    0 S  0.0  0.0   0:00.00 cqueue/1                                                               
  149 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khubd                                                                  
  151 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kseriod                                                                
  219 root      20   0     0    0    0 S  0.0  0.0   0:03.01 khungtaskd                                                             
  222 root      10  -5     0    0    0 S  0.0  0.0   0:34.93 kswapd0                                                                
  223 root      14  -5     0    0    0 S  0.0  0.0   0:00.00 aio/0                                                                  
  224 root      14  -5     0    0    0 S  0.0  0.0   0:00.00 aio/1                                                                  
  396 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kpsmoused                                                              
  426 root      13  -5     0    0    0 S  0.0  0.0   0:00.00 ata/0                                                                  
  427 root      13  -5     0    0    0 S  0.0  0.0   0:00.00 ata/1                                                                  
  428 root      13  -5     0    0    0 S  0.0  0.0   0:00.00 ata_aux                                                                
  432 root      13  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0                                                              
  433 root      12  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_1                                                              
  434 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_2                                                              
  435 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_3                                                              
  439 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_4                                                              
  440 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_5                                                              
  444 root      12  -5     0    0    0 S  0.0  0.0   0:00.00 kstriped                                                              

q 
[root@web ~]#

 

示例二 用于监视特定进程的脚本

文件mytop.sh。只需要将需要监视的进程命令行特征保存到KEYS中即可。

Bash代码   收藏代码
  1. #!/bin/sh  
  2.   
  3. KEYS='agent|omc4j|terminal|module'  
  4.   
  5. pslist()  
  6. {  
  7.     ps -ef | egrep $KEYS | grep -v grep  
  8. }  
  9.   
  10. readpid()  
  11. {  
  12. #root      2823  2822  0 10:28 pts/0    00:00:00 java -classpath ./omc4j.jar:.:../../lib/hyjc.jar:../../lib/log4j-1.2.15.jar omc.test.Run  
  13. #root      2834  2395  0 10:28 pts/1    00:00:01 ./agent  
  14. #root      2841  2507  0 10:28 pts/3    00:00:00 ./terminal  
  15.     while read user pid dummy;  
  16.     do  
  17.         echo -n " -p $pid"  
  18.     done  
  19. }  
  20.   
  21. top `pslist | readpid`  

 

示例三 批处理模式

[root@smsgw root]# top -b -n 1 >top.txt 
[root@smsgw root]# wc -l top.txt 
    728 top.txt
[root@smsgw root]# less top.txt 


 10:44:12  up 340 days, 17:29,  5 users,  load average: 0.41, 0.11, 0.03
713 processes: 708 sleeping, 1 running, 4 zombie, 0 stopped
CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle
           total    0.0%    0.0%    1.1%   0.0%     0.0%    0.0%   98.8%
           cpu00    0.0%    0.0%    0.0%   0.0%     0.0%    0.0%  100.0%
           cpu01    0.0%    0.0%    0.0%   0.0%     0.0%    0.0%  100.0%
           cpu02    0.0%    0.0%    0.0%   0.0%     0.0%    0.0%  100.0%
           cpu03    0.0%    0.0%    4.7%   0.0%     0.0%    0.0%   95.2%
Mem:  2053816k av, 2028696k used,   25120k free,       0k shrd,  160236k buff
                   1260028k actv,  237468k in_d,   32116k in_c
Swap: 2044056k av, 1190484k used,  853572k free                  366668k cached

  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME CPU COMMAND
16039 root      20   0  2004 2004   876 R     1.1  0.0   0:00   3 top
    1 root      15   0   220  188   160 S     0.0  0.0   2:09   2 init
    2 root      RT   0     0    0     0 SW    0.0  0.0   0:00   0 migration/0
    3 root      RT   0     0    0     0 SW    0.0  0.0   0:00   1 migration/1
    4 root      RT   0     0    0     0 SW    0.0  0.0   0:00   2 migration/2
    5 root      RT   0     0    0     0 SW    0.0  0.0   0:00   3 migration/3
    6 root      15   0     0    0     0 SW    0.0  0.0   0:00   0 keventd
    7 root      34  19     0    0     0 SWN   0.0  0.0   0:00   0 ksoftirqd/0
    8 root      34  19     0    0     0 SWN   0.0  0.0   0:00   1 ksoftirqd/1
    9 root      34  19     0    0     0 SWN   0.0  0.0   0:00   2 ksoftirqd/2
   10 root      34  19     0    0     0 SWN   0.0  0.0   0:00   3 ksoftirqd/3
   13 root      15   0     0    0     0 SW    0.0  0.0   0:01   1 bdflush
   11 root      15   0     0    0     0 SW    0.0  0.0  31:07   2 kswapd
   12 root      15   0     0    0     0 SW    0.0  0.0  72:42   1 kscand
   14 root      15   0     0    0     0 SW    0.0  0.0   7:15   0 kupdated
   15 root      25   0     0    0     0 SW    0.0  0.0   0:00   0 mdrecoveryd
   23 root      15   0     0    0     0 SW    0.0  0.0  18:13   2 kjournald
   79 root      24   0     0    0     0 SW    0.0  0.0   0:00   2 khubd
 1158 root      15   0     0    0     0 SW    0.0  0.0   0:00   0 kjournald
 1159 root      15   0     0    0     0 SW    0.0  0.0  11:04   3 kjournald
 1160 root      15   0     0    0     0 SW    0.0  0.0   2:35   2 kjournald
 1161 root      15   0     0    0     0 SW    0.0  0.0   0:00   3 kjournald
 1588 root      15   0   344  340   260 S     0.0  0.0   3:06   2 syslogd
 1592 root      15   0   220  208   160 S     0.0  0.0   0:00   1 klogd
 1603 root      15   0   252  244   192 S     0.0  0.0  14:15   2 irqbalance
top.txt

q

[root@smsgw root]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值