Linux实战:如何查看服务器进程占用,快速定位资源消耗异常

原创 zhaoshuman  2026-04-28 14:47:19  阅读 29 次 评论 0 条
摘要:

本文用于快速查看和分析进程的资源消耗

进程与系统监控

进程管理核心命令 ps - 进程快照分析

# 全格式显示所有进程

root@server:~# ps aux | head -10
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.3 167548 11504 ?        Ss    2025   1:56 /lib/systemd/systemd --system --deserialize 48 noibrs
root           2  0.0  0.0      0     0 ?        S     2025   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<    2025   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<    2025   0:00 [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<    2025   0:00 [slub_flushwq]
root           6  0.0  0.0      0     0 ?        I<    2025   0:00 [netns]
root           8  0.0  0.0      0     0 ?        I<    2025   0:00 [kworker/0:0H-events_highpri]
root          10  0.0  0.0      0     0 ?        I<    2025   0:00 [mm_percpu_wq]
root          11  0.0  0.0      0     0 ?        S     2025   0:00 [rcu_tasks_rude_]

# 树状结构显示进程关系

root@server:~# ps auxf | grep -E '(nginx|apache)'
root     1952087  0.0  0.0   6480  2552 pts/0    S+   11:04   0:00      |   \_ grep --color=auto -E (nginx|apache)
root     1904076  0.0  0.3  55800 13184 ?        S    06:45   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 1904079  0.0  0.1  56552  6624 ?        S    06:45   0:00  \_ nginx: worker process
www-data 1904080  0.0  0.1  56552  6624 ?        S    06:45   0:00  \_ nginx: worker process

# 自定义输出格式(CPU和内存排序)

root@server:~# ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu | head -15
    PID    PPID CMD                         %CPU %MEM
   2073       1 /usr/local/aegis/aegis_clie  2.4  1.3
   1578    1453 ./MediaServer -s default.pe  0.8  0.6
1905031  471143 sshd: root@pts/0,pts/1       0.5  0.3
   1551    1432 java -Djava.security.egd=fi  0.3 13.5
   1007       1 /usr/local/aegis/aegis_clie  0.2  0.3
   1553    1472 mysqld --character-set-serv  0.1 10.4
   1571    1446 redis-server *:6379          0.1  0.0
1905175 1905133 top                          0.1  0.1
      1       0 /lib/systemd/systemd --syst  0.0  0.3
      2       0 [kthreadd]                   0.0  0.0
      3       2 [rcu_gp]                     0.0  0.0
      4       2 [rcu_par_gp]                 0.0  0.0
      5       2 [slub_flushwq]               0.0  0.0
      6       2 [netns]                      0.0  0.0

# 实战:查找内存泄漏进程

root@server:~# ps aux --sort=-%mem | awk 'NR<=10{printf "%-8s %-6s %s\n", $1, $4, $11}'
USER     %MEM   COMMAND
root     13.5   java
lxd      10.4   mysqld
1000     3.6    8fdf65d387f2
root     2.4    /lib/systemd/systemd-journald
root     1.3    /usr/local/aegis/aegis_client/aegis_12_81/AliYunDunMonitor
root     0.7    /usr/libexec/fwupd/fwupd
root     0.7    /sbin/multipathd
root     0.6    /usr/bin/python3
root     0.6    ./MediaServer

top 与 htop - 实时性能监控

# top交互式命令

# P - 按CPU排序,M - 按内存排序,T - 按时间排序

# k - 杀死进程,r - 修改nice值

top - 11:05:46 up 127 days, 18:23,  2 users,  load average: 0.04, 0.09, 0.08
Tasks: 155 total,   1 running, 154 sleeping,   0 stopped,   0 zombie
%Cpu0  :   2.7/2.3     5[|||||                                                                                               ]
%Cpu1  :   2.0/2.0     4[||||                                                                                                ]
MiB Mem : 48.1/3576.7   [||||||||||||||||||||||||||||||||||||||||||||||||                                                    ]
MiB Swap:  0.0/0.0      [                                                                                                    ]
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                            
   2073 root      20   0  178224  47940      0 S   4.3   1.3   4469:11 AliYunDunMonito                                                                                                                                    
   1578 root      20   0  461004  22716   1700 S   1.0   0.6   1643:35 MediaServer                                                                                                                                        
   1599 1000      20   0 1238036 134412    172 S   0.7   3.7 146:04.33 beam.smp                                                                                                                                           
   1007 root      20   0   97632  13844   6116 S   0.3   0.4 412:48.98 AliYunDun                                                                                                                                          
   1551 root      20   0 3815708 497204    308 S   0.3  13.6 575:32.33 java                                                                                                                                               
1905031 root      20   0   17744  12040   9104 S   0.3   0.3   0:24.57 sshd                                                                                                                                               
1952935 root      20   0   10812   4580   3808 R   0.3   0.1   0:00.02 top                                                                                                                                                
      1 root      20   0  167548  11504   6720 S   0.0   0.3   1:56.51 systemd                                                                                                                                            
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.45 kthreadd                                                                                                                                           
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                                                                                                                                             
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                                                                                                                                         
      5 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 slub_flushwq                                                                                                                                       
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 netns                                                                                                                                              
      8 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-events_highpri                                                                                                                        
     10 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq                                                                                                                                       
     11 root      20   0       0      0      0 S   0.0   0.0   0:00.00 rcu_tasks_rude_                                                                                                                                    
     12 root      20   0       0      0      0 S   0.0   0.0   0:00.00 rcu_tasks_trace                                                                                                                                    
     13 root      20   0       0      0      0 S   0.0   0.0   3:20.55 ksoftirqd/0                                                                                                                                        
     14 root      20   0       0      0      0 I   0.0   0.0  66:40.81 rcu_sched                                                                                                                                          
     15 root      rt   0       0      0      0 S   0.0   0.0   0:16.36 migration/0                                                                                                                                        
     16 root     -51   0       0      0      0 S   0.0   0.0   0:00.00 idle_inject/0                                                                                                                                      
     18 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/0                                                                                                                                            
     19 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/1

top -p $(pgrep -d',' nginx)  # 仅监控nginx进程

top - 11:06:14 up 127 days, 18:23,  2 users,  load average: 0.10, 0.10, 0.09
Tasks:   3 total,   0 running,   3 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.0 us,  1.7 sy,  0.0 ni, 96.1 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
MiB Mem :   3576.7 total,    111.1 free,   1440.6 used,   2025.1 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   1855.1 avail Mem 
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                            
1904076 root      20   0   55800  13184  10972 S   0.0   0.4   0:00.00 nginx                                                                                                                                              
1904079 www-data  20   0   56552   6624   4016 S   0.0   0.2   0:00.00 nginx                                                                                                                                              
1904080 www-data  20   0   56552   6624   4016 S   0.0   0.2   0:00.00 nginx

# htop高级功能(需安装)

htop -u www-data  # 仅显示特定用户进程

htop -t  # 显示进程树

TRAE IDE效率提升:在TRAE IDE的终端中,可以直接使用htop的彩色输出,配合智能命令补全功能,快速定位性能瓶颈进程。


2. ps — 静态快照,适合脚本化

当需要批量输出或集成到脚本中时,ps 比 top 更灵活:

bash

复制

# 查看占用 CPU 最高的 10 个进程

ps aux --sort=-%cpu | head -10
root@server:~# ps aux --sort=-%cpu | head -10
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        2073  2.4  1.3 178224 47940 ?        Ssl   2025 4469:16 /usr/local/aegis/aegis_client/aegis_12_81/AliYunDunMonitor
root        1578  0.8  0.6 461004 22716 ?        Ssl   2025 1643:36 ./MediaServer -s default.pem -c ../conf/config.ini -l 0
root     1905031  0.5  0.3  17744 12040 ?        Ss   09:54   0:25 sshd: root@pts/0,pts/1
root        1551  0.3 13.5 3815708 497204 ?      Ssl   2025 575:32 java -Djava.security.egd=file:/dev/./urandom -jar wvp-pro.jar
root        1007  0.2  0.3  97632 13844 ?        Ssl   2025 412:49 /usr/local/aegis/aegis_client/aegis_12_81/AliYunDun
lxd         1571  0.1  0.1  55388  3696 ?        Ssl   2025 191:36 redis-server *:6379
root     1905175  0.1  0.1  11856  5184 pts/1    S+   09:54   0:07 top
root           1  0.0  0.3 167548 11504 ?        Ss    2025   1:56 /lib/systemd/systemd --system --deserialize 48 noibrs

# 查看占用内存最高的 10 个进程

ps aux --sort=-%mem | head -10
root@server:~# ps aux --sort=-%mem | head -10
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        1551  0.3 13.5 3815708 497204 ?      Ssl   2025 575:32 java -Djava.security.egd=file:/dev/./urandom -jar wvp-pro.jar
root      526775  0.0  2.4 383836 89316 ?        S<s  Mar25   2:04 /lib/systemd/systemd-journald
root        2073  2.4  1.3 178224 47940 ?        Ssl   2025 4469:16 /usr/local/aegis/aegis_client/aegis_12_81/AliYunDunMonitor
root     1929518  0.0  0.7 386544 27452 ?        Ssl  10:30   0:00 /usr/libexec/fwupd/fwupd
root         431  0.0  0.7 289312 27096 ?        SLsl  2025   8:05 /sbin/multipathd -d -s
root     1154764  0.0  0.6 261412 23220 ?        Ssl  Apr16   1:23 /usr/bin/python3 -Es /usr/sbin/tuned -l -P
root        1578  0.8  0.6 461004 22716 ?        Ssl   2025 1643:36 ./MediaServer -s default.pem -c ../conf/config.ini -l 0

# 自定义输出格式(PID、命令、CPU、内存)

ps -eo pid,cmd,%cpu,%mem --sort=-%cpu | head -15
root@server:~# ps -eo pid,cmd,%cpu,%mem --sort=-%cpu | head -15
    PID CMD                         %CPU %MEM
   2073 /usr/local/aegis/aegis_clie  2.4  1.3
   1578 ./MediaServer -s default.pe  0.8  0.6
1905031 sshd: root@pts/0,pts/1       0.5  0.3
   1551 java -Djava.security.egd=fi  0.3 13.5
   1007 /usr/local/aegis/aegis_clie  0.2  0.3
   1553 mysqld --character-set-serv  0.1 10.4
   1571 redis-server *:6379          0.1  0.1
1905175 top                          0.1  0.1
      1 /lib/systemd/systemd --syst  0.0  0.3
      2 [kthreadd]                   0.0  0.0
      3 [rcu_gp]                     0.0  0.0
      4 [rcu_par_gp]                 0.0  0.0
      5 [slub_flushwq]               0.0  0.0
      6 [netns]                      0.0  0.0

# 查看特定服务的所有进程

ps auxf | grep -E '(nginx|java|mysql)'
root@server:~# ps auxf | grep -E '(nginx|java|mysql)'
root        1551  0.3 13.5 3815708 497204 ?      Ssl   2025 575:32  \_ java -Djava.security.egd=file:/dev/./urandom -jar wvp-pro.jar
lxd         1553  0.1 10.4 1818220 382896 ?      Ssl   2025 340:14  \_ mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=mysql_native_password --explicit_defaults_for_timestamp=0 --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION --explicit_defaults_for_timestamp=0
root     1954784  0.0  0.0   6612  2372 pts/0    S+   11:08   0:00      |   \_ grep --color=auto -E (nginx|java|mysql)
root     1904076  0.0  0.3  55800 13160 ?        S    06:45   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 1904079  0.0  0.1  56552  6600 ?        S    06:45   0:00  \_ nginx: worker process
www-data 1904080  0.0  0.1  56552  6600 ?        S    06:45   0:00  \_ nginx: worker process

批量查看多维度资源占用:

ps -eo pid,ppid,cmd,%cpu,%mem,nlwp --sort=-%cpu | head -10
root@server:~# ps -eo pid,ppid,cmd,%cpu,%mem,nlwp --sort=-%cpu | head -10
    PID    PPID CMD                         %CPU %MEM NLWP
   2073       1 /usr/local/aegis/aegis_clie  2.4  1.3   30
   1578    1453 ./MediaServer -s default.pe  0.8  0.6    7
1905031  471143 sshd: root@pts/0,pts/1       0.5  0.3    1
   1551    1432 java -Djava.security.egd=fi  0.3 13.5  354
   1007       1 /usr/local/aegis/aegis_clie  0.2  0.3   12
   1553    1472 mysqld --character-set-serv  0.1 10.4   44
   1571    1446 redis-server *:6379          0.1  0.0    5
1905175 1905133 top                          0.1  0.1    1
      1       0 /lib/systemd/systemd --syst  0.0  0.3    1

nlwp 列显示线程数,适合排查线程泄漏问题 。

#进程PID查询,必须是唯一进程

查询进程运行时长:ps -p `pgrep -f python|awk 'NR==4'` -o etime|awk 'NR==2 {print $1}'

root@server:~# ps -p `pgrep -f python|awk 'NR==1'` -o etime|awk 'NR==2 {print $1}'
9-17:44:22

#如上:显示进程已经运行9天,17小时44分22秒

本文地址:https://zhaoshuman.cn/shell/17.html
免责声明:本文为原创文章,版权归 zhaoshuman 所有,欢迎分享本文,转载请保留出处!
NEXT:已经是最新一篇了

发表评论


表情

还没有留言,还不快点抢沙发?