(5)ps详解 (每周一个linux命令系列)

时间:2021-09-10 01:12:57

(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: xxx@pts/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

(5)ps详解 (每周一个linux命令系列)的更多相关文章

  1. &lpar;4&rpar;top详解 &lpar;每周一个linux命令系列&rpar;

    (4)top详解 (每周一个linux命令系列) linux命令 top详解 引言:今天的命令是用来看cpu信息的top top 我们先看man top top - display Linux pro ...

  2. &lpar;3&rpar;lscpu详解 &lpar;每周一个linux命令系列&rpar;

    (3)lscpu详解 (每周一个linux命令系列) linux命令 lscpu详解 引言:今天的命令是用来看cpu信息的lscpu lscpu 我们先看man lscpu display infor ...

  3. &lpar;2&rpar;free详解 &lpar;每周一个linux命令系列&rpar;

    (2)free详解 (每周一个linux命令系列) linux命令 free详解 引言:今天的命令是用来看内存的free free 换一个套路,我们先看man free中对free的描述: Displ ...

  4. &lpar;6&rpar;sudo命令详解(每周一个linux命令系列)

    首先说句抱歉,最近事情比较复杂,停更了一阵子.我又回来啦 多用户管理 我们常用的windows个人系统虽然可以设置多用户,但是实际上是不可以多用户同时登陆的(这个我实验过,我以前用windows服务器 ...

  5. &lpar;7&rpar;awk命令(每周一个linux命令系列)

    简介 awk是一个强大的文本分析工具,尤其是在应对格式化比较好的日志文件时,简单来说awk就是把文件逐行的读入,以空格为默认分隔符(也可以指定分隔符)将每行切片处理. 语法 awk [选项参数] 's ...

  6. 每周一个linux命令之---uptime详解

    每周一个linux命令之---uptime详解 linux命令 uptime详解 引言:从今天开始,每周更新一个对程序员有用的linux命令,我真的没敢写每天一个,我怕我坚持不下去,每周一个还是可以的 ...

  7. 企业sudo权限规划详解 &lpar;实测一个堆命令搞定&rpar;

    简述问题:         随着公司的服务器越来越多,人员流动性也开始与日俱增,以往管理服务器的陈旧思想应当摒弃,公司需要有 更好更完善的权限体系,经过多轮沟通和协商,公司一致决定重新整理规划权限体系 ...

  8. (11)nc命令(每周一个linux命令)

    nc(netcat)实用程序几乎可用于所有涉及TCP或UDP的事情.它可以打开TCP连接,发送UDP数据包,监听任意TCP和UDP端口,进行端口扫描,处理IPv4和IPv6.与telnet不同,nc可 ...

  9. 【转载】每天一个Linux命令

    目  录 每天一个linux命令(1)  : ls 命令 每天一个linux命令(2)  : cd 命令 每天一个linux命令(3)  : pwd 命令 每天一个linux命令(4)  : mkdi ...

随机推荐

  1. IOS开发基础知识--碎片21

    1:[UIScreen mainScreen].scale知识点 当屏幕分别为640x940时[[UIScreen mainScreen] scale]=2.0 当屏幕分别为320x480时[[UIS ...

  2. vim编辑技巧

    算上大学上课linux接触四年了,可惜除了工作接触的那些,其他地方还是很水的,都不精通,今日翻来看了看以前过目即忘的vim技巧,发现,如无几年操作经验,这些技巧很难领悟的.这里写下来,以备后查. 区块 ...

  3. CSS3绘制404页面

    标题有点噱了...最近在做一个交通有关的项目, 想做一个类似标志牌的404, 所以就有了这个. 只用的CSS3中的旋转, 效果如下 上代码: <!DOCTYPE html> <htm ...

  4. Mesos编译步骤及部署注意事项(Ubuntu)

    注意事项: 编译过程如果有错误提示少什么库,则相应的安装库即可在编译中出现 g++: internal compiler error: Killed (program cc1plus)的错误是因为内存 ...

  5. js实现类似于add&lpar;1&rpar;&lpar;2&rpar;&lpar;3&rpar;调用方式的方法

    群里有人说实现类似add(1)(2)(3)调用方式的方法,结果马上有人回答: var add = function(a){ return function(b){ return function(c) ...

  6. closures

     一.什么是闭包和闭包的几种写法和用法 1.什么是闭包 闭包,官方对闭包的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.闭包的特点: 1. ...

  7. &lbrack;leetcode&rsqb;92&period; Reverse Linked List II反转链表2

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  8. MapReduce业务 - 图片关联计算

    1.概述 最近在和人交流时谈到数据相似度和数据共性问题,而刚好在业务层面有类似的需求,今天和大家分享这类问题的解决思路,分享目录如下所示: 业务背景 编码实践 预览截图 下面开始今天的内容分享. 2. ...

  9. 荣耀实锤Magic2或将助力AI,再次带动成长?

    临近年底,热闹了一年的手机圈纷纷偃旗息鼓,准备为明年3月的新品发力.然而今天(12月7日),恰逢节气大雪,@荣耀手机 在微博发布了一张预热海报,随后荣耀总裁赵明转发这条微博表示「关于技术,真的有很多话 ...

  10. Feeling&lowbar;2018&lowbar;5&lowbar;22

    “我打你,你会走吗?”    “不会!!” “我骂你,你会走吗?”    “不会!!” “那我不爱你了,你会走吗?”       “会.”