|
| 1 | +ps 备忘清单 |
| 2 | +=== |
| 3 | + |
| 4 | +Linux 为我们提供了一个名为 `ps` 的实用程序,用于查看与系统上的进程相关的信息,它是 `Process Status` 的缩写 |
| 5 | +这份 `ps` 命令备忘清单的快速参考列表,包含常用选项和示例。 |
| 6 | + |
| 7 | +入门 |
| 8 | +--- |
| 9 | + |
| 10 | +### 语法 |
| 11 | +<!--rehype:wrap-class=row-span-4--> |
| 12 | + |
| 13 | +```bash |
| 14 | +$ ps [options] |
| 15 | +``` |
| 16 | + |
| 17 | +命令运行示例,列出当前 shell 中的所有进程: |
| 18 | + |
| 19 | +```bash |
| 20 | +$ ps |
| 21 | + |
| 22 | + PID TTY TIME CMD |
| 23 | +12330 pts/0 00:00:00 bash |
| 24 | +21621 pts/0 00:00:00 ps |
| 25 | +``` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +:-- | -- |
| 30 | +:-- | -- |
| 31 | +`PID` | 唯一的进程 ID |
| 32 | +`TTY` | 用户登录的终端类型 |
| 33 | +`TIME` | 进程运行的 CPU 数量,以分钟和秒为单位 |
| 34 | +`CMD` | 启动进程的命令的名称 |
| 35 | + |
| 36 | +注意:有时当我们执行 `ps` 命令时,它显示 `TIME` 为 `00:00:00` |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +ps 命令支持 3 种使用语法风格 |
| 41 | + |
| 42 | +- `Unix` 可以分组并以连字符开头 |
| 43 | +- `BSD` 可以分组但前面没有连字符 |
| 44 | +- `GNU` 长选项,前面有双连字符 |
| 45 | + |
| 46 | +### 示例 |
| 47 | +<!--rehype:wrap-class=row-span-3--> |
| 48 | + |
| 49 | +Option | Function |
| 50 | +:-- | -- |
| 51 | +`ps -ef / -aux` | 以完整格式列出当前正在运行的进程 |
| 52 | +`ps -ax` | 列出当前正在运行的进程 |
| 53 | +`ps -u <username>` | 列出特定用户的进程 |
| 54 | +`ps -C <command>` | 列出给定命令的进程 |
| 55 | +`ps -p <PID>` | 列出具有给定 PID 的进程 |
| 56 | +`ps -ppid <PPID>` | 列出具有给定 ppid 的进程 |
| 57 | +`pstree` | 在层次结构中显示过程 |
| 58 | +`ps -L` | 列出特定进程的所有线程 |
| 59 | +`ps --sort pmem` | 查找内存泄漏 |
| 60 | +`ps -eo` | 显示安全信息 |
| 61 | +`ps T` | 允许您仅选择与此终端关联的所有进程 |
| 62 | +`ps -U root -u root u` | 显示由 root 运行的进程 |
| 63 | +<!--rehype:className=code-nowrap--> |
| 64 | + |
| 65 | +### 查看系统上的每个进程 |
| 66 | + |
| 67 | +要使用标准语法查看系统上的每个进程: |
| 68 | + |
| 69 | +```bash |
| 70 | +$ ps -e # 列出所有进程 |
| 71 | +$ ps -ef |
| 72 | +$ ps -eF |
| 73 | +$ ps -ely |
| 74 | +``` |
| 75 | + |
| 76 | +要使用 BSD 语法查看系统上的每个进程: |
| 77 | + |
| 78 | +```bash |
| 79 | +$ ps ax # 以 BSD 格式列出所有进程 |
| 80 | +$ ps axu |
| 81 | +``` |
| 82 | + |
| 83 | +### 打印进程树 |
| 84 | + |
| 85 | +```bash |
| 86 | +$ ps -ejH |
| 87 | +$ ps axjf |
| 88 | +``` |
| 89 | + |
| 90 | +### 仅打印 PID 42 的名称 |
| 91 | + |
| 92 | +```bash |
| 93 | +$ ps -q 42 -o comm= |
| 94 | +``` |
| 95 | + |
| 96 | +### 获取有关线程的信息 |
| 97 | + |
| 98 | +```bash |
| 99 | +$ ps -eLf |
| 100 | +$ ps axms |
| 101 | +``` |
| 102 | + |
| 103 | +### 列出当前用户拥有的所有进程 |
| 104 | + |
| 105 | +```bash |
| 106 | +$ ps x |
| 107 | +``` |
| 108 | + |
| 109 | +### 获取安全信息 |
| 110 | +<!--rehype:wrap-class=col-span-2--> |
| 111 | + |
| 112 | +```bash |
| 113 | +$ ps -eo euser,ruser,suser,fuser,f,comm,label |
| 114 | +$ ps axZ |
| 115 | +$ ps -eM |
| 116 | +``` |
| 117 | + |
| 118 | +### 查看以 root 身份运行的每个进程 |
| 119 | + |
| 120 | +查看以 root 身份运行的每个进程(真实且有效的 ID)用户格式: |
| 121 | + |
| 122 | +```bash |
| 123 | +$ ps -U root -u root u |
| 124 | +``` |
| 125 | + |
| 126 | +### 查看具有用户定义格式的每个进程 |
| 127 | +<!--rehype:wrap-class=col-span-2--> |
| 128 | + |
| 129 | +```bash |
| 130 | +$ ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm |
| 131 | +$ ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm |
| 132 | +$ ps -Ao pid,tt,user,fname,tmout,f,wchan |
| 133 | +``` |
| 134 | + |
| 135 | +### 仅打印 syslogd 的进程 ID |
| 136 | + |
| 137 | +```bash |
| 138 | +$ ps -C syslogd -o pid= |
| 139 | +``` |
| 140 | + |
| 141 | +### 显示面向用户的格式 |
| 142 | +<!--rehype:wrap-class=col-span-2 row-span-2--> |
| 143 | + |
| 144 | +```bash |
| 145 | +$ ps u |
| 146 | + |
| 147 | +USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND |
| 148 | +refs 11400 1.1 0.0 34853544 5816 s025 Ss Tue02PM 0:02.82 /bin/zsh --login |
| 149 | +refs 34561 0.6 0.0 34822644 3152 s016 S+ 14Dec22 115:59.28 zsh (figterm) |
| 150 | +refs 21377 0.5 0.0 34973972 7076 s028 S+ Wed09AM 4:32.19 zsh (figterm) |
| 151 | +refs 78881 0.5 0.0 34843484 3256 s015 S+ 17Dec22 90:27.10 zsh (figterm) |
| 152 | +``` |
| 153 | + |
| 154 | +### 列出具有完整格式的进程 |
| 155 | + |
| 156 | +```bash |
| 157 | +$ ps f |
| 158 | +$ ps -F |
| 159 | +``` |
| 160 | + |
| 161 | +### 显示虚拟内存格式 |
| 162 | + |
| 163 | +```bash |
| 164 | +$ ps v |
| 165 | +``` |
| 166 | + |
| 167 | +### 按有效用户 ID 或名称显示进程 |
| 168 | + |
| 169 | +```bash |
| 170 | +$ ps -u user[name or id] |
| 171 | +# OR |
| 172 | +$ ps --user user[name or id] |
| 173 | +$ ps -u root |
| 174 | +``` |
| 175 | + |
| 176 | +按**真实**用户 ID 或名称显示进程 |
| 177 | + |
| 178 | +```bash |
| 179 | +$ ps -U user[name or id] |
| 180 | +# OR |
| 181 | +$ ps --User user[name or id] |
| 182 | +``` |
| 183 | + |
| 184 | +### 按实际组 ID 或名称显示进程 |
| 185 | + |
| 186 | +```bash |
| 187 | +$ ps -G group[name or id] |
| 188 | +# OR |
| 189 | +$ ps --Group group[name or id] |
| 190 | +``` |
| 191 | + |
| 192 | +### 隐藏 ps 命令输出的标题 |
| 193 | + |
| 194 | +```bash |
| 195 | +$ ps h |
| 196 | + |
| 197 | + PID TT STAT TIME COMMAND |
| 198 | +33790 s000 S+ 104:10.45 zsh (figterm) |
| 199 | +33800 s001 Ss+ 0:02.76 /bin/zsh --login |
| 200 | +77830 s002 S+ 90:22.51 zsh (figterm) |
| 201 | +77840 s003 Ss 0:00.66 /bin/zsh --login |
| 202 | +``` |
| 203 | + |
| 204 | +### 显示命令后的环境 |
| 205 | + |
| 206 | +```bash |
| 207 | +$ ps e |
| 208 | + |
| 209 | + PID TTY STAT TIME COMMAND |
| 210 | + 886 tty2 Ssl+ 0:00 /usr/li.... |
| 211 | +``` |
| 212 | + |
| 213 | +### 重复 ps 命令输出的标题行 |
| 214 | + |
| 215 | +```bash |
| 216 | +$ ps --headers -A |
| 217 | + PID TTY TIME CMD |
| 218 | + 1 ? 00:00:01 systemd |
| 219 | + 2 ? 00:00:00 kthreadd |
| 220 | + 3 ? 00:00:00 rcu_gp |
| 221 | +``` |
| 222 | + |
| 223 | +### 显示进程树 |
| 224 | + |
| 225 | +```bash |
| 226 | +$ ps --forest -A |
| 227 | + PID TTY TIME CMD |
| 228 | + 2 ? 00:00:00 kthreadd |
| 229 | + 3 ? 00:00:00 \_ rcu_gp |
| 230 | + 4 ? 00:00:00 \_ rcu_par_gp |
| 231 | + 960 ? 00:00:00 \_ goa-identity-se |
| 232 | + 1118 ? 00:00:00 \_ at-spi-bus-laun |
| 233 | + 1124 ? 00:00:00 | \_ dbus-daemon |
| 234 | +``` |
| 235 | + |
| 236 | +您可以使用 -H 选项打印进程层次结构 |
| 237 | + |
| 238 | +```bash |
| 239 | +$ ps -H -A |
| 240 | +PID TTY TIME CMD |
| 241 | + 2 ? 00:00:00 kthreadd |
| 242 | + 3 ? 00:00:00 rcu_gp |
| 243 | +1832 ? 00:00:37 gnome-terminal- |
| 244 | +1840 pts/0 00:00:00 bash |
| 245 | +1925 pts/1 00:00:00 bash |
| 246 | +2867 pts/1 00:00:00 su |
| 247 | +2868 pts/1 00:00:00 bash |
| 248 | +``` |
0 commit comments