procps(1)包介绍
procps
是一个在 Linux 系统中广泛使用的软件包,主要提供了一系列用于监控和管理系统进程的工具。它是许多 Linux 发行版(包括 Ubuntu)的核心组件之一。procps
的工具主要依赖于 /proc
文件系统(Linux 内核提供的虚拟文件系统),通过读取其中的信息来展示系统状态、进程信息等。
在 Ubuntu 中,procps
包通常默认安装,因为它包含了一些基础且常用的命令行工具,例如 ps
、top
、kill
等。如果没有安装,可以通过以下命令安装:
1 | sudo apt update |
procps info
root@vgt-ubuntu-10:~# apt info procps
Package: procps
Version: 2:3.3.17-6ubuntu2.1
Priority: required
Section: admin
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Craig Small <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 1421 kB
Provides: watch
Depends: libc6 (>= 2.34), libncurses6 (>= 6), libncursesw6 (>= 6), libprocps8 (>= 2:3.3.16-1), libtinfo6 (>= 6), lsb-base (>= 3.0-10), init-system-helpers (>= 1.29~)
Recommends: psmisc
Breaks: guymager (<= 0.5.9-1), manpages-de (<< 4.9.1-2), manpages-fr (<< 4.9.1-2), manpages-fr-extra (<< 20151231+nmu1), manpages-pl (<< 1:4.9.1-2), open-vm-tools (<= 2011.12.20-562307-1)
Replaces: manpages-de (<< 4.9.1-2), manpages-fr (<< 4.9.1-2), manpages-fr-extra (<< 20151231+nmu1), manpages-pl (<< 1:4.9.1-2)
Homepage: https://gitlab.com/procps-ng/procps
Task: minimal, server-minimal
Download-Size: 378 kB
APT-Manual-Installed: no
APT-Sources: http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
### 包介绍
Description: /proc file system utilities
This package provides command line and full screen utilities for browsing
procfs, a "pseudo" file system dynamically generated by the kernel to
provide information about the status of entries in its process table
(such as whether the process is running, stopped, or a "zombie").
.
It contains free, kill, pkill, pgrep, pmap, ps, pwdx, skill, slabtop,
snice, sysctl, tload, top, uptime, vmstat, w, and watch.
procps包内命令
root@vgt-ubuntu-10:~# dpkg -L procps |grep bin # 查看已安装包内文件
/bin
/bin/kill
/bin/ps
/sbin
/sbin/sysctl
/usr/bin
/usr/bin/free
/usr/bin/pgrep
/usr/bin/pidwait
/usr/bin/pmap
/usr/bin/pwdx
/usr/bin/skill
/usr/bin/slabtop
/usr/bin/tload
/usr/bin/top
/usr/bin/uptime
/usr/bin/vmstat
/usr/bin/w
/usr/bin/watch
/usr/bin/pkill
/usr/bin/snice
1.procps包含的主要工具
procps
包提供以下常用工具:
ps
- 显示当前系统进程的快照。top
- 实时监控系统进程和资源使用情况。kill
- 向指定进程发送信号(通常用于终止进程)。free
- 显示系统内存使用情况。uptime
- 显示系统运行时间和负载。w
- 显示当前登录用户及其正在执行的任务。pgrep
- 根据进程名查找进程 ID。pkill
- 根据进程名发送信号终止进程。vmstat
- 报告虚拟内存统计信息。sysctl
- 配置内核参数(运行时修改/proc/sys
中的值)。
2.使用示例
1.ps
- 查看进程
1 | ps aux |
a
:显示所有用户的进程。u
:显示详细信息(包括用户、CPU 使用率等)。x
:包括没有控制终端的进程。
输出示例:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user1 1234 0.1 0.5 12345 6789 ? S 10:00 0:01 /usr/bin/python3
2. top
- 实时监控
1 | top |
- 启动后,界面会实时更新,显示 CPU、内存使用情况和进程列表。
- 按
q
退出。
3. kill
- 终止进程
1 | kill -9 1234 |
-9
表示发送 SIGKILL 信号,强制终止 PID 为 1234 的进程。
4. free
- 查看内存
1 | free -h |
-h
:以人类可读的格式(如 GB、MB)显示内存使用情况。
输出示例:
total used free shared buff/cache available
Mem: 7.8G 2.3G 3.1G 0.2G 2.4G 5.2G
Swap: 2.0G 0.0G 2.0G
5. uptime
- 查看系统运行时间
1 | uptime |
输出示例:
10:15:23 up 2 days, 3:45, 1 user, load average: 0.15, 0.10, 0.08
6. pgrep
和 pkill
- 按进程名操作
查找进程:
1 | pgrep python |
输出:返回所有运行 Python 进程的 PID,例如 1234 5678
。
终止进程:
1 | pkill -9 python |
- 强制终止所有名为
python
的进程。
7. vmstat
- 虚拟内存统计
1 | vmstat 2 |
- 每 2 秒刷新一次,显示内存、CPU 和 I/O 的统计信息。
8. sysctl
- 修改内核参数
查看所有参数:
1 | sysctl -a |
修改参数(例如增大最大文件描述符数量):
1 | sudo sysctl -w fs.file-max=100000 |
3.实际应用场景
- 性能监控:使用
top
或vmstat
检查 CPU 和内存使用情况,找出占用资源过高的进程。 - 进程管理:通过
ps
找到目标进程的 PID,然后用kill
或pkill
终止不需要的进程。 - 内存分析:用
free
检查可用内存,判断是否需要清理缓存或增加 Swap。 - 系统调试:结合
uptime
和w
,了解系统负载和用户活动。