linux ps命令基础

ps命令的参数非常多 通常只需要记住理解主要的几个即可

一、ps命令介绍

  Linux系统中会运行各种各样的进程,如果要对进程进行监控,首先必须了解当前进程的情况,也就是需要查看当前进程。ps命令就是最基本的进程查看命令,使用该命令可以看到有哪些进程正在运行,并且可以看到进程的运行状态进程是否结束进程有没有僵死哪些进程占用了过多的资源等等。总之大部分信息都是可以通过执行该命令得到。ps是显示执行命令瞬间进程的状态,并不动态连续,如果想对进程进行实时监控应该用top命令

  ps命令的格式支持 3种不同类型的命令行参数:

  1、Unix风格的参数,前面加"-"

  2、BSD风格的参数,前面不加"-"

  3、GNU风格的长参数,前面加"--"

 

Unix风格参数

参数 描述
-e 显示所有进程
-l  显示长列表    
-f  显示完整格式的输出
[user1@Centos8_test ~]$ ps -ef |head -5
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 01:08 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
root           2       0  0 01:08 ?        00:00:00 [kthreadd]
root           3       2  0 01:08 ?        00:00:00 [rcu_gp]
root           4       2  0 01:08 ?        00:00:00 [rcu_par_gp]
[user1@Centos8_test ~]$ ps -efl |head -5
F S UID          PID    PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root           1       0  0  80   0 - 61590 -      01:08 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
1 S root           2       0  0  80   0 -     0 -      01:08 ?        00:00:00 [kthreadd]
1 I root           3       2  0  60 -20 -     0 -      01:08 ?        00:00:00 [rcu_gp]
1 I root           4       2  0  60 -20 -     0 -      01:08 ?        00:00:00 [rcu_par_gp]

参数:

UID:启动这些进程的用户。
PID:进程的进程ID。
PPID:父进程的进程号(如果该进程是由另一个进程启动的)。
C:进程生命周期中的CPU利用率。
STIME:进程启动时的系统时间。
TTY:进程启动时的终端设备。
TIME:运行进程需要的累计CPU时间。
CMD:启动的程序名称。

加上 l 参数之后 会产生一个长格式输出:

F:内核分配给进程的系统标记。
S:进程的状态(O代表正在运行;S代表在休眠;R代表可运行,正等待运行;Z代表僵化,进程已结束但父进程已不存在;T代表停止)。
PRI:进程的优先级(越大的数字代表越低的优先级)。
NI:谦让度值用来参与决定优先级。
ADDR:进程的内存地址。
SZ:假如进程被换出,所需交换空间的大致大小。
WCHAN:进程休眠的内核函数的地址。

BSD风格

参数 描述
a 显示跟任意终端关联的所有进程
u 采用基于用户的格式显示
x 显示所有的进程,甚至包括未分配任何终端的进程
l 采用长格式
#当前打开了三个终端 用户名为root的两个 用户名为user1的一个
[root@Centos8_test ~]# ps u #显示基于当前终端用户格式
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        1224  0.0  0.0  13632  1812 tty1     Ss+  11:33   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root        1789  0.0  0.1  27660  5484 pts/0    Ss   11:40   0:00 -bash
root        1916  0.0  0.1  27660  5116 pts/1    Ss+  11:41   0:00 -bash
root        1975  0.0  0.0  62936   444 pts/1    S+   11:41   0:00 dbus-launch --sh-syntax --exit-with-session
root        4534  0.0  0.1  58692  3996 pts/0    R+   11:47   0:00 ps u
[root@Centos8_test ~]# ps au # 加上a之后 显示和任意终端关联的所有进程
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        1224  0.0  0.0  13632  1812 tty1     Ss+  11:33   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root        1789  0.0  0.1  27660  5484 pts/0    Ss   11:40   0:00 -bash
root        1916  0.0  0.1  27660  5116 pts/1    Ss+  11:41   0:00 -bash
root        1975  0.0  0.0  62936   444 pts/1    S+   11:41   0:00 dbus-launch --sh-syntax --exit-with-session
user1       2028  0.0  0.1  25540  5296 pts/2    Ss+  11:41   0:00 -bash
user1       2132  0.0  0.0  62936  2516 pts/2    S+   11:41   0:00 dbus-launch --sh-syntax --exit-with-session
root        4903  0.0  0.1  58692  3980 pts/0    R+   11:48   0:00 ps au
[user1@Centos8_test ~]$ ps aux |head -n 20 # x参数显示所有进程 包括未分配任何终端的进程
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.1  0.5 246232 15148 ?        Ss   11:33   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
root           2  0.0  0.0      0     0 ?        S    11:33   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   11:33   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   11:33   0:00 [rcu_par_gp]
root           6  0.0  0.0      0     0 ?        I<   11:33   0:00 [kworker/0:0H-kblockd]
root           8  0.0  0.0      0     0 ?        I    11:33   0:00 [kworker/u256:0-events_unbound]
root           9  0.0  0.0      0     0 ?        I<   11:33   0:00 [mm_percpu_wq]
root          10  0.0  0.0      0     0 ?        S    11:33   0:00 [ksoftirqd/0]
root          11  0.0  0.0      0     0 ?        I    11:33   0:00 [rcu_sched]
root          12  0.0  0.0      0     0 ?        S    11:33   0:00 [migration/0]
root          13  0.0  0.0      0     0 ?        S    11:33   0:00 [watchdog/0]
root          14  0.0  0.0      0     0 ?        S    11:33   0:00 [cpuhp/0]
root          15  0.0  0.0      0     0 ?        S    11:33   0:00 [cpuhp/1]
root          16  0.0  0.0      0     0 ?        S    11:33   0:00 [watchdog/1]
root          17  0.0  0.0      0     0 ?        S    11:33   0:00 [migration/1]
root          18  0.0  0.0      0     0 ?        S    11:33   0:00 [ksoftirqd/1]
root          20  0.0  0.0      0     0 ?        I<   11:33   0:00 [kworker/1:0H-kblockd]
root          21  0.0  0.0      0     0 ?        S    11:33   0:00 [cpuhp/2]
root          22  0.0  0.0      0     0 ?        S    11:33   0:00 [watchdog/2]

GNU长参数

--forest 以层级结构显示出进程与父进程之间的关系。

[user1@Centos8_test ~]$ bash
[user1@Centos8_test ~]$ bash
[user1@Centos8_test ~]$ bash
[user1@Centos8_test ~]$ ps --forest
    PID TTY          TIME CMD
   2028 pts/2    00:00:00 bash
   7205 pts/2    00:00:00  \_ bash
   7237 pts/2    00:00:00      \_ bash
   7281 pts/2    00:00:00          \_ bash
   7351 pts/2    00:00:00              \_ ps
   2132 pts/2    00:00:00 dbus-launch

 

posted @ 2022-08-02 01:04  zed99  阅读(102)  评论(0编辑  收藏  举报