ps详解 (每周一个linux命令系列)
Posted vinter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ps详解 (每周一个linux命令系列)相关的知识,希望对你有一定的参考价值。
(5)ps详解 (每周一个linux命令系列)
linux命令 ps详解
引言:今天的命令是用来看进程状态的ps命令
ps
我们先看man ps
ps - report a snapshot of the current processes.
翻译:显示当前进程的快照。ps是 Process Status的缩写
具体的描述如下:
DESCRIPTION
ps displays information about a selection of the active processes. If you
want a repetitive update of the selection and the displayed information, use
top(1) instead.
ps展示了当前运行的程序的一些选项信息快照,如果想要查看实时更新的程序信息,请使用top命令。
This version of ps accepts several kinds of options:
ps可以接收多种不同的选项
1 UNIX options, which may be grouped and must be preceded by a dash.
2 BSD options, which may be grouped and must not be used with a dash.
2 GNU long options, which are preceded by two dashes.
1 UNIX选项,需要分组使用,并且必须以破折号开头。
2 BSD选项,需要分组使用,不得与破折号一起使用。
3 GNU长选项,前面有两个破折号。
Options of different types may be freely mixed, but conflicts can appear.
There are some synonymous options, which are functionally identical, due to
the many standards and ps implementations that this ps is compatible with.
不同类型的选项可以自由混合,但可能出现冲突。
由于ps兼容的许多标准和ps实现,有一些在功能上相同的同义选项。
Note that "ps -aux" is distinct from "ps aux". The POSIX and UNIX standards
require that "ps -aux" print all processes owned by a user named "x", as well
as printing all processes that would be selected by the -a option. If the
user named "x" does not exist, this ps may interpret the command as "ps aux"
instead and print a warning. This behavior is intended to aid in
transitioning old scripts and habits. It is fragile, subject to change, and
thus should not be relied upon.
请注意,“ps -aux”与“ps aux”不同。
POSIX和UNIX标准要求“ps -aux”打印名为“x”的用户拥有的所有进程,
以及打印将由-a选项选择的所有进程。如果名为“x”的用户不存在,则此ps可能会将
该命令解释为“ps aux”且打印警告。此行为旨在帮助转换旧脚本和习惯。
这不一定是可靠的,尽量不要这样用
By default, ps selects all processes with the same effective user ID
(euid=EUID) as the current user and associated with the same terminal as the
invoker. It displays the process ID (pid=PID), the terminal associated with
the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format
(time=TIME), and the executable name (ucmd=CMD). Output is unsorted by
default.
默认情况下,ps选择与当前用户具有相同有效用户ID(euid = EUID)且与调用者相同的终端关联的所有进程。
它显示进程ID(pid = PID),与进程关联的终端(tname = TTY),
累计CPU时间[DD-] hh:mm:ss格式(time = TIME)和可执行文件名(ucmd) = CMD)。
默认情况下,不对输出排序。
The use of BSD-style options will add process state (stat=STAT) to the
default display and show the command args (args=COMMAND) instead of the
executable name. You can override this with the PS_FORMAT environment
variable. The use of BSD-style options will also change the process selection
to include processes on other terminals (TTYs) that are owned by you;
alternately, this may be described as setting the selection to be the set of all processes filtered to exclude processes owned by other users or not on a terminal. These effects are not considered when options are described as being "identical" below, so -M will be considered identical to Z and so on.
使用BSD样式选项会将进程状态(stat = STAT)添加到默认显示,并显示命令args(args = COMMAND)
而不是可执行文件名。您可以使用PS_FORMAT环境变量覆盖它。使用BSD风格的选项也会改变进程的选择,
以包括您拥有的其他终端(TTY)上的进程;或者,这可以被描述为将选择过滤设置为排除其他用户所拥有的和
不在终端上的进程的集合。当选项在下面被描述为“相同”时,不需要考虑这些效果,因此-M将被视为与Z相同
依此类推。
Except as described below, process selection options are additive. The
default selection is discarded, and then the selected processes are added to the set of processes to be displayed. A process will thus be shown if it meets any of the given selection criteria.
除以下描述外,进程选择选项是附加的。将丢弃默认选择,然后将所选进程添加到要显示的进程集。因此,如果满足任何给定的选择标准,将显示该进程。
我们再来看一下帮助文档
dev1:~$ ps --help all
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 e.g. ‘-u root,nobody‘ or
a blank-separated list e.g. ‘-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
打印所有用户的运行进程
a 显示所有用户的进程
u 显示进程的所有者
x 显示非终端启用的进程
注意之前介绍了 aux和-au的区别
输出:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 28726 0.0 0.0 0 0 ? S 10:42 0:00 [kworker/3:0]
root 28889 0.0 0.0 0 0 ? S 10:44 0:00 [kworker/0:2]
root 28979 0.0 0.0 0 0 ? S 10:45 0:00 [kworker/2:2]
root 29014 0.0 0.0 0 0 ? S 10:45 0:00 [kworker/1:0]
列属性说明 其他的列参考man ps吧
- USER:该进程属于那个使用者账号的
- PID :该进程的号码
- %CPU:该进程所使用的CPU时间百分比。
- %MEM:该进程所使用的CPU时间百分比
- VSZ :该进程占用的虚拟内存量 (Kbytes)
- RSS :该进程占用的固定的内存量 (Kbytes)
- TTY :该进程是在那个终端机上面运作,若与终端机无关,则显示 ?,
另外, tty1-tty6 是本机上面的登入者程序,若为 pts/0等等的,则表示为由网络连接进主机的程序。
- STAT:Process Status 该程序目前的状态,主要的状态有
D :不可中断的睡眠. 通常是处于I/O之中
R :该程序目前正处于运行队列中.运行中/可运行.
S :该程序目前正在睡眠当中,但可被某些事件或讯号 (signal) 触发唤醒。
T :已停止. 可能是因为shell作业控制或进程正被调试.
Z :该程序应该已经终止,但是其父程序没有终止他,造成 zombie (疆尸) 程序的状态
注:进程状态还有其他的几种但是一般不常用
- START :该 process 被触发启动的时间
- TIME :该 process 实际使用 CPU 运作的时间
- COMMAND :该程序的实际指令
接下来说一些常用命令
最常用的就是和grep配合 查找特定的进程了
ps -ef | grep ssh
输出
root 1026 1 0 10月02 ? 00:00:08 /usr/sbin/sshd -D
root 1534 1026 0 10月08 ? 00:00:00 sshd: xxx [priv]
xxx 1536 1534 0 10月08 ? 00:00:01 sshd: [email protected]/1,pts/2,pts/3
定制输出的列属性
我们可以使用-o来定制列属性
ps -o uid,%cpu,%mem,pid,ppid,pgrp,session,tpgid,comm
查看特定用户的进程
在需要查看特定用户进程的情况下,我们可以使用 -u 参数。比如查看vinter用户的进程
ps -u vinter
对输出结果进行排序
升序和降序是通过在字段前面增加负号来设置的 有负号‘-’代表降序
ps --sort ppid 升序
ps --sort -ppid 降序
查看某个名称的进程
例如查看名为ps的进程
ps -C ps
查看某个PID的进程
ps -P 23178
把ps的结果输出到文件或分页查看
可以用 | 管道和 more 连接起来分页查看。
ps aux |more
把ps结果输出到ps.txt文本。
ps aux > ./ps.txt
以上是关于ps详解 (每周一个linux命令系列)的主要内容,如果未能解决你的问题,请参考以下文章
精通Linux系列服务器之间的telnet与scp命令用法,进程管理命令之ps -ef与ps aux详解
精通Linux系列服务器之间的telnet与scp命令用法,进程管理命令之ps -ef与ps aux详解