在 CentOS 7 中,管理进程是系统维护的重要部分,进程是指程序在运行后在系统中产生的记录,包括该进程占用的资源,如cpu、内存等
1.静态查询
1.ps -ef
会列出系统中 所有用户 的 所有进程,包括进程的父进程 ID、启动时间、执行路径等。
[root@root nginx]# ps -ef | head
UID PID PPID C STIME TTY TIME CMD
root 2 0 0 08:49 ? 00:00:00 [kthreadd]
UID 用户id
PID 进程id
PPID 父进程ID
C 占用CPU的百分比
STIME 进程启动时间
TTY 运行终端
TIME 占用CPU时间
CMD 程序名
2.ps aux
会列出系统中 所有用户的所有进程,并显示进程的 CPU 占用率、内存占用率、启动时间等实时信息。
[root@root nginx]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 08:49 0:00 [kthreadd]
USER 用户名
PID 进程ID
%CPU CPU占用率
%MEM 内存占用率 memory
VSZ 虚拟内存
RSS 实际消耗内存
TTY 运行终端
STAT 进程状态
START 启动时间
TIME 占用COU时间
COMMAND 程序名
2.动态查询
1.top
是一个实时监控进程和系统资源的强大工具,能够动态显示 CPU、内存、进程状态等信息。
[root@root nginx]# top
top - 17:41:14 up 8:51, 4 users, load average: 0.00, 0.01, 0.05
Tasks: 222 total, 1 running, 221 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 3.1 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 995772 total, 177884 free, 643168 used, 174720 buff/cache
KiB Swap: 2097148 total, 1999612 free, 97536 used. 181960 avail MemPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 191436 2960 1356 S 0.0 0.3 0:02.02 systemd
top - 17:41:14 up 8:51, 4 users load average: 0.00, 0.01, 0.05
当前系统时间 运行时长 登录用户 平均负载:1分钟 5分钟 15分钟
Tasks: 188 total, 1 running, 187 sleeping, 0 stopped, 0 zombie
进程总量 1个运行 187个休眠 0个停止 0个僵尸
0.0 us, 3.1 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
用户态 内核态 nice值 空闲率 等待硬盘百分比 硬中断 软中断 虚拟化偷取
KiB Mem : 995772 total, 177884 free, 643168 used, 174720 buff/cache
内存总量 空闲内存 使用内存 缓存 有效内存
KiB Swap: 2097148 total, 1999612 free, 97536 used.
交换分区 总共 空闲 使用
查询后台进程
[root@root ~]# jobs
后台程序运行
[root@root ~]# bg 3
后台程序调到前台
[root@root ~]# fg 3
nice值
-20~19 值越小越优先
[root@root ~]# nice -n -20 vim
[root@root ~]# renice -10 -p 45136 #调整pid未45136的进行nice值为-10
2.uptime
它会实时获取系统当前的运行状态
15:07:23 up 1 day, 7:40, 4 users, load average: 0.00, 0.01, 0.05
3.mpstat
[root@root ~]# mpstat
Linux 3.10.0-1062.el7.x86_64 (root.com) 2025年03月31日 _x86_64_ (2 CPU)18时04分01秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
18时04分01秒 all 0.10 0.00 0.12 0.01 0.00 0.00 0.00 0.00 0.00 99.77
4.lscpu
查看cpu详细信息
[root@root ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 2
座: 1
NUMA 节点: 1
厂商 ID: GenuineIntel
CPU 系列: 6
型号: 141
型号名称: 11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz
步进: 1
CPU MHz: 2688.001
BogoMIPS: 5376.00
超管理器厂商: VMware
虚拟化类型: 完全
L1d 缓存: 48K
L1i 缓存: 32K
L2 缓存: 1280K
L3 缓存: 12288K
NUMA 节点0 CPU: 0,1
Flags: 略
5.free
查看内存
[root@root ~]# free -m
total used free shared buff/cache available
Mem: 972 627 173 23 171 177
Swap: 2047 95 1952
[root@root ~]# free
total used free shared buff/cache available
Mem: 995772 642708 177396 23772 175668 181956
Swap: 2097148 97536 1999612