一、命令简介

  Linux ps (英文全称:process status)命令用于显示当前进程的状态,类似于 windows 的任务管理器。ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命令。要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而 ps 命令就是最基本同时也是非常强大的进程查看命令。使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等。总之大部分信息都是可以通过执行该命令得到的。

二、使用示例

1、获取命令帮助

[root@test1 opt]# ps --help all

linux中的xvf linux中的ps命令_ps

2、显示指定命令进程

[root@test1 ~]# sleep 100 &
[1] 44898
[root@test1 ~]# ps -C sleep
PID TTY TIME CMD
44879 ? 00:00:00 sleep
44898 pts/0 00:00:00 sleep
[root@test1 ~]# ps -ef |grep 44898
root 44898 44533 0 13:59 pts/0 00:00:00 sleep 100
root 44915 44533 0 13:59 pts/0 00:00:00 grep --color=auto 44898

3、显示指定用户进程

[root@test1 ~]# ps -u es
PID TTY TIME CMD
15530 ? 00:13:07 java
15554 ? 00:00:00 controller
127992 ? 00:06:48 npm run start
128003 ? 00:00:00 grunt

4、显示指定用户组进程

[root@test1 ~]# ps -g es
PID TTY TIME CMD
15530 ? 00:13:07 java
15554 ? 00:00:00 controller
127992 ? 00:06:48 npm run start
128003 ? 00:00:00 grunt

5、使用标准语法查看进程

[root@test1 ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jun18 ? 00:01:09 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0 0 Jun18 ? 00:00:00 [kthreadd]
root 3 2 0 Jun18 ? 00:00:22 [ksoftirqd/0]
root 5 2 0 Jun18 ? 00:00:00 [kworker/0:0H]
root 7 2 0 Jun18 ? 00:00:00 [migration/0]
root 8 2 0 Jun18 ? 00:00:00 [rcu_bh]
root 9 2 0 Jun18 ? 00:00:31 [rcu_sched]
root 10 2 0 Jun18 ? 00:00:00 [lru-add-drain]
root 11 2 0 Jun18 ? 00:00:05 [watchdog/0]

6、使用BSD语法查看系统上的每个进程

[root@test1 ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 125604 1912 ? Ss Jun18 1:09 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0.0 0.0 0 0 ? S Jun18 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Jun18 0:22 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Jun18 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Jun18 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S Jun18 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R Jun18 0:31 [rcu_sched]
root 10 0.0 0.0 0 0 ? S< Jun18 0:00 [lru-add-drain]

7、显示进程树

[root@test1 ~]# ps axjf

linux中的xvf linux中的ps命令_shell命令_02


[root@test1 ~]# ps -ejH


linux中的xvf linux中的ps命令_ps_03

8、显示线程信息

[root@test1 ~]# ps -eLf

linux中的xvf linux中的ps命令_shell命令_04

9、按照设置显示指定列

[root@test1 ~]# ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm

linux中的xvf linux中的ps命令_参数说明_05

10、按照指定关键字列排序

[root@test1 ~]# ps kuser -ef

linux中的xvf linux中的ps命令_linux中的xvf_06

三、使用语法及参数说明

1、使用语法

用法: ps [options]

2、参数说明

参数

参数说明

-A, -e

显示所有进程

-a

显示除了会话主进程外的所有tty下的进程

a

显示tty下的所有进程,包括其他用户

-d

显示除了会话主进程外的所有进程

-N, --deselect

取消选择

r

只显示运行进程

T

显示终端下的所有进程

x

显示不包括tty用户的所有进程

-C

显示command命令进程

-G, --Group

显示指定gid用户组进程

-g, --group

显示指定用户组名称进程

-p, p, --pid

显示指定进程号进程

–ppid

显示pid的子进程

-q, q, --quick-pid

显示进程ID(quick mode)

-s, --sid

显示会话IP进程

-t, t, --tty

显示指定tty终端进程

-u, U, --user

显示指定有效userid或者用户名进程

-U, --User

显示指定实际userid或者用户名进程

H

像进程一样显示线程

-L

显示线程可能包含LWP和NLWP行

-m, m

在进程后显示线程

-T

显示线程,可能包含SPID行

-c

与-l选项一起显示调度类

c

显示真实命令名

e

显示环境变量

k, --sort

指定排序方式

L

显示格式说明符

n

显示数字uid和wchan

S, --cumulative

包含一些死子进程数据

-y

不显示标志,显示rss(仅使用-l)

-V, V, --version

显示命令版本

-w, w

不限制输入宽度

–help <simple

list

3、排序关键字

关键字简写

关键字

说明

c

cmd

simple name of executable

C

pcpu

cpu utilization

f

flags

flags as in long format F field

g

pgrp

process group ID

G

tpgid

controlling tty process group ID

j

cutime

cumulative user time

J

cstime

cumulative system time

k

utime

user time

m

min_flt

number of minor page faults

M

maj_flt

number of major page faults

n

cmin_flt

cumulative minor page faults

N

cmaj_flt

cumulative major page faults

o

session

session ID

p

pid

process ID

P

ppid

parent process ID

r

rss

resident set size

R

resident

resident pages

s

size

memory size in kilobytes

S

share

amount of shared pages

t

tty

the device number of the controlling tty

T

start_time

time process was started

U

uid

user ID number

u

user

user name

v

vsize

total VM size in KiB

y

priority

kernel scheduling priority