博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux基础 - 进程管理 ps

Posted on 2023-02-19 13:39  Kingdomer  阅读(27)  评论(0编辑  收藏  举报

 

ps aux | sort -k4,4nr | head -n 10  # 查看内存占用前10名的程序

ps -e / ps -ef / ps -eF / ps -ely   # To see every process on the system using standard syntax

ps ax / ps axu                      # To see every process on the system using BSD syntax

ps -ejH / ps axjf                   # To print a process tree

ps -eLf / ps axms                   # To get info about threads

 

查看当前用户的活跃进程

[root@cl-server ~]# ps
   PID TTY          TIME CMD
  1656 pts/0    00:00:00 httpd
  1855 pts/0    00:00:00 bash
  2901 pts/0    00:00:23 java
  3034 pts/0    00:00:00 ps

  

UID:    程序被该 UID 所拥有
PID:    程序的ID 
PPID:   上级父程序的ID
C:      CPU使用的资源百分比, CPU用于计算执行优先级的因子
STIME:  程序启动时间
TTY:    进程所属的终端控制台, 问号表示这些进程不属于任何TTY, 它们是由系统启动的。
TIME:   程序使用的CPU时间
CMD:    程序的指令
[root@server ~]# ps -ef | grep salt UID PID PPID C STIME TTY TIME CMD root 2899 1 0 2021 ? 00:09:14 /usr/bin/python /usr/bin/salt-master root 3050 2899 0 2021 ? 00:00:00 /usr/bin/python /usr/bin/salt-master root 3272 2899 0 2021 ? 00:00:00 /usr/bin/python /usr/bin/salt-master root 3275 2899 0 2021 ? 00:00:03 /usr/bin/python /usr/bin/salt-master root 3276 2899 0 2021 ? 13:13:45 /usr/bin/python /usr/bin/salt-master root 3277 2899 0 2021 ? 00:09:08 /usr/bin/python /usr/bin/salt-master root 3278 3277 0 2021 ? 00:00:04 /usr/bin/python /usr/bin/salt-master root 3288 3277 0 2021 ? 00:02:35 /usr/bin/python /usr/bin/salt-master root 3289 3277 0 2021 ? 00:02:47 /usr/bin/python /usr/bin/salt-master root 3290 3277 0 2021 ? 00:02:40 /usr/bin/python /usr/bin/salt-master root 3292 2899 0 2021 ? 1-13:21:45 /usr/bin/python /usr/bin/salt-master root 3293 3277 0 2021 ? 00:02:33 /usr/bin/python /usr/bin/salt-master root 3295 3277 0 2021 ? 00:02:13 /usr/bin/python /usr/bin/salt-master root 3182 6423 0 09:59 pts/1 00:00:00 grep --color=auto salt

  

-a: 显示所有用户的进程    -l: long format,展示更多信息

VSZ: 进程的虚拟内存    RSS: 常驻内存的大小

STAT状态值:  S-睡眠,s-表示该进程是会话的先导进程,N-表示进程拥有比普通优先级更低的优先级,R-正在运行,D-短期等待,Z-僵死进程, T-被跟踪或者被停止等等

[soupman@myos ~]$ ps -lax 
F   UID     PID    PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0       1       0  20   0 179336  7924 -      Ss   ?         12:48 /usr/lib/systemd/systemd --switched-root --system --deseri
1     0       2       0  20   0      0     0 -      S    ?          0:00 [kthreadd]

 

[root@cl-server ~]#  ps axu
USER        PID %CPU %MEM    VSZ     RSS TTY   STAT START   TIME COMMAND
root          1  0.0  0.2 125464    3916   ?   Ss   14:28   0:02 /usr/lib/systemd/systemd --switched-root --system --deserializ
root          2  0.0  0.0      0       0   ?   S    14:28   0:00 [kthreadd]
root        692  0.0  0.0 110204     856 tty1  Ss+  14:28   0:00 /sbin/agetty --noclear tty1 linux
mysql       957  0.1 10.4 1183028 195308   ?   Ssl  14:28   0:07 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
root       2901  1.6 20.2 3658356 377928   ?   Sl   15:37   0:25 /usr/local/jdk1.8.0_281/bin/java -Xms64M -Xmx1G -Djava.util.lo

 

[root@cl-server ~]# ps -ef f
UID         PID   PPID  C STIME TTY      STAT   TIME CMD
root          2      0  0 14:28 ?        S      0:00 [kthreadd]
root          4      2  0 14:28 ?        S<     0:00  \_ [kworker/0:0H]
root       1082      1  0 14:28 ?        Ssl    0:02 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root       1557   1082  0 14:28 ?        Sl     0:00  \_ /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8899 -cont
root       1572   1082  0 14:28 ?        Sl     0:00  \_ /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8899 -container

  

TTY 展示探究

说明:当前cl-server服务器,通过VMware Workstation 界面启动,通过xshell工具开启两个会话。
启动终端: tty1, 会话终端: pts/0, pts/1。 在pts/0上启动 activemq 服务。
[root@cl-server ~]# ps ax
  PID TTY      STAT   TIME COMMAND
     1 ?        Ss     0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
     2 ?        S      0:00 [kthreadd]
     4 ?        S<     0:00 [kworker/0:0H]
   692 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux
  1656 pts/0    Ss+    0:00 /bin/httpd -f -h /data/html
  1689 ?        Ss     0:00 /bin/httpd -f -h /data/html
  1853 ?        Ss     0:00 sshd: root@pts/0
  1855 pts/0    Ss     0:00 -bash
  1951 ?        R      0:00 [kworker/1:1]
  2472 ?        Ss     0:00 sshd: root@pts/1
  2474 pts/1    Ss+    0:00 -bash
  2901 pts/0    Sl     0:24 /usr/local/jdk1.8.0_281/bin/java -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -
  3102 pts/0    R+     0:00 ps ax

关闭会话pts/0, TTY变成?
[root@cl-server ~]# ps ax
   PID TTY      STAT   TIME COMMAND
  1656 ?        Ss+    0:00 /bin/httpd -f -h /data/html
  1689 ?        Ss     0:00 /bin/httpd -f -h /data/html
  2901 ?        Sl     0:25 /usr/local/jdk1.8.0_281/bin/java -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -
  3156 pts/1    R+     0:00 ps ax

 

       To get security info:
          ps -eo euser,ruser,suser,fuser,f,comm,label
          ps axZ
          ps -eM

       To see every process running as root (real & effective ID) in user format:
          ps -U root -u root u

       To see every process with a user-defined format:
          ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
          ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
          ps -Ao pid,tt,user,fname,tmout,f,wchan

       Print only the process IDs of syslogd:
          ps -C syslogd -o pid=

       Print only the name of PID 42:
          ps -q 42 -o comm=

  

[root@cl-server ~]# 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.

For more details see ps(1).
[root@cl-server ~]# 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

For more details see ps(1).