关注公众号 “融码一生”,领取全套 PDF / 电子书
- ps 命令是最常用的监控进程的命令,该命令可以查看系统中所有运行进程的详细信息。
- 不同 Linux 发行版的 ps 命令的语法各不相同,为此,Linux 采取了一个折中的方法,融合各种不同的风格,兼顾那些已经习惯其它系统上使用 ps 命令的用户。
- 进程是一个运行中的、具有一定独立功能的程序,是操作系统动态执行的基本单元。
- 常用选项(选项可以不加
-
):
选项 |
含义 |
-a |
显示终端上的所有进程,包括其他用户的进程(除会话引线外) |
-e |
显示所有进程 |
-l |
长格式显示更加详细的信息 |
-u |
显示进程的详细状态(进程的归属用户及内存的使用情况) |
-x |
显示没有控制终端的进程 |
-w |
显示加宽,以便显示更多的信息 |
-r |
只显示正在运行的进程 |
- ps 命令默认只会输出当前用户并且是当前终端(如当前 Shell)下调用的进程的信息:
[test1@VM-4-13-centos ~]$ ps
:<<!
PID TTY TIME CMD
1842 pts/3 00:00:00 bash
4288 pts/3 00:00:00 ps
!
-
- PID:进程 ID
- TTY:与进程关联的终端
- TIME:格式为
[dd-]hh:mm:ss
的进程累积 CPU 时间 - CMD:可执行文件的名称
- 输出内容默认是不排序的
- 使用标准语法显示系统中的每个进程:
[test1@VM-4-13-centos ~]$ ps -ef | head -2
:<<!
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jul29 ? 00:00:56 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
!
- 使用 BSD 语法显示系统中的每个进程:
[test1@VM-4-13-centos ~]$ ps aux | head -2
:<<!
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 44620 3992 ? Ss Jul29 0:56 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
!
- 使用 BSD 语法会默认增加显示进程状态(STAT)等信息,也可以使用
PS_FORMAT
环境变量重写默认的输出格式。
1、ps aux
:查看系统中所有的进程
[test1@VM-4-13-centos ~]$ ps -a
:<<!
PID TTY TIME CMD
777 pts/0 00:00:00 find
4194 pts/1 00:00:00 ps
!
# jy: 查看系统中所有的进程, 使用 BS 操作系统格式(以下两个命令等价)
[test1@VM-4-13-centos ~]$ ps -aux
[test1@VM-4-13-centos ~]$ ps aux
:<<!
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 52816 4012 ? Ss Jul29 1:06 /usr/lib/systemd/systemd --s
root 2 0.0 0.0 0 0 ? S Jul29 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< Jul29 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S Jul29 0:18 [ksoftirqd/0]
root 7 0.0 0.0 0 0 ? S Jul29 0:15 [migration/0]
root 8 0.0 0.0 0 0 ? S Jul29 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S Jul29 2:16 [rcu_sched]
root 10 0.0 0.0 0 0 ? S< Jul29 0:00 [lru-add-drain]
root 11 0.0 0.0 0 0 ? S Jul29 0:01 [watchdog/0]
root 12 0.0 0.0 0 0 ? S Jul29 0:01 [watchdog/1]
...
!
-
ps aux
输出信息中各列的含义:
-
- USER:该进程是由哪个用户产生的
- PID:进程 ID
- %CPU:该进程占用 CPU 资源的百分比
- %MEM:该进程占用物理内存的百分比
- VSZ:该进程占用虚拟内存的大小(单位为 KB)
- RSS:该进程占用实际物理内存的大小(单位为 KB)
- TTY:该进程是在哪个终端运行的
-
-
- tty1 ~ tty7 代表本地控制台终端(可以通过 Alt+F1 ~ F7 快捷键切换不同终端)
-
-
-
-
- tty1 ~ tty6 是本地的字符界面终端
- tty7 是图形终端
-
-
-
-
- pts/0 ~ 255 代表虚拟终端,一般是远程连接的终端(第一个远程连接占用 pts/0,第二个远程连接占用 pts/1,依次类推)
-
-
- STAT:进程状态。常见有以下几种:
-
-
- -D:不可被唤醒的睡眠状态,通常用于 I/O 情况
- -R:该进程正在运行
- -S:该进程处于睡眠状态,可被唤醒
- -T:停止状态,可能是在后台暂停或进程处于除错状态
- -W:内存交互状态(从 2.6 内核开始无效)
- -X:死掉的进程(应该不会出现)
- -Z:僵尸进程。进程已经中止,但是部分程序还在内存当中
- -<:高优先级(以下状态在 BSD 格式中出现)
- -N:低优先级
- -L:被锁入内存
- -s:包含子进程
- -l:多线程(小写 L)
- -+:位于后台
-
-
- START:该进程的启动时间
- TIME:该进程占用 CPU 的运算时间(注意:不是系统时间)
- COMMAND:产生此进程的命令名
2、ps -le
:查看系统中所有的进程
-
ps -le
也能看到系统中所有的进程。-l
选项能看到更详细的信息,比如父进程 PID(PPID)、优先级(PRI)等。
# jy: 查看系统中所有的进程, 使用 Linux 标准命令格式
[test1@VM-4-13-centos ~]$ ps -le
:<<!
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 1 0 0 80 0 - 13204 ep_pol ? 00:01:06 systemd
1 S 0 2 0 0 80 0 - 0 kthrea ? 00:00:00 kthreadd
1 S 0 4 2 0 60 -20 - 0 worker ? 00:00:00 kworker/0:0H
1 S 0 6 2 0 80 0 - 0 smpboo ? 00:00:18 ksoftirqd/0
1 S 0 7 2 0 -40 - - 0 smpboo ? 00:00:15 migration/0
1 S 0 8 2 0 80 0 - 0 rcu_gp ? 00:00:00 rcu_bh
1 S 0 9 2 0 80 0 - 0 rcu_gp ? 00:02:16 rcu_sched
1 S 0 10 2 0 60 -20 - 0 rescue ? 00:00:00 lru-add-drain
5 S 0 11 2 0 -40 - - 0 smpboo ? 00:00:01 watchdog/0
...
!
-
ps -le
输出信息中各列的含义:
-
- F:进程标志(说明进程的权限),常见的标志有:
-
-
- 1:进程可以被复制,但不能被执行
- 4:进程使用超级用户权限
-
-
- S:进程状态。具体状态和
ps aux
命令中的 STAT 状态一致 - UID:运行此进程的用户的 ID
- PID:进程 ID
- PPID:父进程的 ID
- C:该进程的 CPU 使用率(单位是 %)
- PRI:进程的优先级(数值越小,优先级越高,越早被 CPU 执行)
- NI:进程的优先级(数值越小,该进程越早被执行)
- ADDR:该进程在内存的哪个位置
- SZ:该进程占用多大内存
- WCHAN:该进程是否运行(
"-"
代表正在运行) - TTY:该进程由哪个终端产生
- TIME:该进程占用 CPU 的运算时间(注意:不是系统时间)
- CMD:产生此进程的命令名
- S:进程状态。具体状态和
3、ps -l
:只查看当前 Shell 产生的进程
[test1@VM-4-13-centos ~]$ ps -l
:<<!
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1001 4121 4118 0 80 0 - 29445 do_wai pts/1 00:00:00 bash
0 R 1001 7506 4121 0 80 0 - 38594 - pts/1 00:00:00 ps
!
- 可以看到,这次从 pts/1 虚拟终端登录,只产生了两个进程:
-
- 登录之后生成的 Shell,也就是 bash
- 正在执行的 ps 命令
- 僵尸进程:由于进程非正常停止或程序编写错误,导致子进程先于父进程结束,而父进程又没有正确地回收子进程,从而造成子进程一直存在于内存当中,这就是僵尸进程。
- 僵尸进程会对主机的稳定性产生影响,所以,在产生僵尸进程后,一定要对产生僵尸进程的软件进行优化,避免一直产生僵尸进程;对于已经产生的僵尸进程,可以在查找出来之后强制中止。
4、ps 常见组合用法
(1)批量删除进程
ps -aux | grep <脚本名称等> | grep -v grep | cut -c 9-15 | xargs kill -9
(2)查看用户开启的线程数
ps -eLf | grep jyhuang | wc -l
(3)查看后台运行的脚本对应的进程号
- 查看后台运行的脚本
对应的进程号:
-
ps -aux | grep
- 查看系统中 httpd 进程的信息:
-
ps aux | grep httpd
5、帮助指令
(1)ps --help
ps --help
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
(2)ps --help s
ps --help s
Usage:
ps [options]
Basic options:
-A, -e all processes
-a all with tty, except session leaders
a all with tty, including other users
-d all except session leaders
-N, --deselect negate selection
r only running processes
T all processes on this terminal
x processes without controlling ttys
(3)ps --help l
ps --help l
Usage:
ps [options]
Selection by list:
-C <command> command name
-G, --Group <GID> real group id or name
-g, --group <group> session or effective group name
-p, p, --pid <PID> process id
--ppid <PID> parent process id
-q, q, --quick-pid <PID>
process id (quick mode)
-s, --sid <session> session id
-t, t, --tty <tty> terminal
-u, U, --user <UID> effective user id or name
-U, --User <UID> real user id or name
The selection options take as their argument either:
a comma-separated list . '-u root,nobody' or
a blank-separated list . '-p 123 4567'
(4)ps --help o
ps --help o
Usage:
ps [options]
Output formats:
-F extra full
-f full-format, including command lines
f, --forest ascii art process tree
-H show process hierarchy
-j jobs format
j BSD job control format
-l long format
l BSD long format
-M, Z add security data (for SELinux)
-O <format> preloaded with default columns
O <format> as -O, with BSD personality
-o, o, --format <format>
user-defined format
s signal format
u user-oriented format
v virtual memory format
X register format
-y do not show flags, show rss vs. addr (used with -l)
--context display security context (for SELinux)
--headers repeat header lines, one per page
--no-headers do not print header at all
--cols, --columns, --width <num>
set screen width
--rows, --lines <num>
set screen height
(5)ps --help t
ps --help t
Usage:
ps [options]
Show threads:
H as if they were processes
-L possibly with LWP and NLWP columns
-m, m after processes
-T possibly with SPID column
(6)ps --help m
ps --help m
Usage:
ps [options]
Miscellaneous options:
-c show scheduling class with -l option
c show true command name
e show the environment after command
k, --sort specify sort order as: [+|-]key[,[+|-]key[,...]]
L show format specifiers
n display numeric uid and wchan
S, --cumulative include some dead child process data
-y do not show flags, show rss (only with -l)
-V, V, --version display version information and exit
-w, w unlimited output width
--help <simple|list|output|threads|misc|all>
display help and exit
(7)ps --help a
ps --help a
Usage:
ps [options]
Basic options:
-A, -e all processes
-a all with tty, except session leaders
a all with tty, including other users
-d all except session leaders
-N, --deselect negate selection
r only running processes
T all processes on this terminal
x processes without controlling ttys
Selection by list:
-C <command> command name
-G, --Group <GID> real group id or name
-g, --group <group> session or effective group name
-p, p, --pid <PID> process id
--ppid <PID> parent process id
-q, q, --quick-pid <PID>
process id (quick mode)
-s, --sid <session> session id
-t, t, --tty <tty> terminal
-u, U, --user <UID> effective user id or name
-U, --User <UID> real user id or name
The selection options take as their argument either:
a comma-separated list . '-u root,nobody' or
a blank-separated list . '-p 123 4567'
Output formats:
-F extra full
-f full-format, including command lines
f, --forest ascii art process tree
-H show process hierarchy
-j jobs format
j BSD job control format
-l long format
l BSD long format
-M, Z add security data (for SELinux)
-O <format> preloaded with default columns
O <format> as -O, with BSD personality
-o, o, --format <format>
user-defined format
s signal format
u user-oriented format
v virtual memory format
X register format
-y do not show flags, show rss vs. addr (used with -l)
--context display security context (for SELinux)
--headers repeat header lines, one per page
--no-headers do not print header at all
--cols, --columns, --width <num>
set screen width
--rows, --lines <num>
set screen height
Show threads:
H as if they were processes
-L possibly with LWP and NLWP columns
-m, m after processes
-T possibly with SPID column
Miscellaneous options:
-c show scheduling class with -l option
c show true command name
e show the environment after command
k, --sort specify sort order as: [+|-]key[,[+|-]key[,...]]
L show format specifiers
n display numeric uid and wchan
S, --cumulative include some dead child process data
-y do not show flags, show rss (only with -l)
-V, V, --version display version information and exit
-w, w unlimited output width
--help <simple|list|output|threads|misc|all>
display help and exit
关注公众号 “融码一生”,领取全套 PDF / 电子书