返回

揭秘Linux命令宝藏:探索top的鲜为人知秘诀

后端

大家还对Linux命令top了解多少呢?它的功能是否仅仅是显示进程的状态呢?当然不止!top命令蕴藏着海量鲜为人知的实用知识,掌握这些秘诀,将大大提高您的Linux操作效率。

在交互模式下,top命令如同一座宝库,等待您去发掘其真正的强大之处。今天,我们就来揭开top命令的面纱,探索它鲜为人知的秘诀。

top命令的秘诀大公开

top命令的秘诀主要体现在其交互模式的各种选项中。进入交互模式后,您只需按下相应的键即可执行特定的操作,高效便捷。

  • j/k键: 上下移动光标,选择进程。
  • r键: 反转进程排序。
  • s键: 根据CPU使用率排序进程。
  • m键: 根据内存使用率排序进程。
  • t键: 按启动时间排序进程。
  • f键: 进入筛选模式,按进程名、用户或其他条件过滤进程。
  • n键: 指定更新频率,以秒为单位。
  • q键: 退出top命令。

实例演示:巧用top命令

下面我们通过一个实例来演示top命令交互模式的强大功能。假设我们有一台运行过载的服务器,我们需要找出耗费大量资源的进程。

top

进入top命令后,我们先按s键 ,将进程按CPU使用率排序,然后按j/k键 找到消耗CPU最高的进程。接着,我们按f键 进入筛选模式,输入进程名或用户等条件进行过滤。最后,我们再按q键 退出top命令。

top - 15:51:18 up 1 day, 23:57,  2 users,  load average: 0.17, 0.13, 0.12
Tasks: 333 total,   1 running, 332 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.5 us,  0.8 sy,  0.0 ni, 93.3 id,  0.4 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8191996 total,  8150112 used,   41884 free,   106712 buffers
KiB Swap:  1574204 total,        0 used,  1574204 free.   1484160 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0  171528  12444   7844 S   0.0  0.2   0:11.62 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/0
    4 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_sched
    5 root      20   0       0      0      0 S   0.0  0.0   0:00.00 migration/0
    6 root      20   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/0
    7 root      20   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/1
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/2
    9 root      20   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/3
   10 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0
   11 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1
   12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/2
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/3
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rapl:0
   15 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rapl:1
   16 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/0
   17 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/1
   18 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/2
   19 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/3
   20 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/4
   21 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/5
   22 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/6
   23 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/7
   24 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/8
   25 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/9
   26 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/10
   27 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/11
   28 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/12
   29 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/13
   30 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/14
   31 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/15
   32 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/16
   33 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/17
   34 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/18
   35 root      20   0       0      0      0 S   0.0  0.0   0:00.00 softirq/19
   36 root      20   0       0      0