Linux工具篇--ps使用介绍

239 阅读3分钟

PS冷门用法: ps是linux进程管理最常用的命令,也是用户和进程管理最实用的命令。为了定位问题和解决问题,往往会用到一些了冷门的用法和参数显示。

AUX输出
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
  • USER: 进程所有者的用户名

  • PID: 进程ID

  • %CPU: 占用的 CPU 使用率

  • %MEM: 进程使用内存的百分比

  • VSZ: 占用的虚拟内存大小

  • RSS: 占用的内存大小

  • TTY: 终端的次要装置号码 (minor device number of tty)

  • STAT: 该行程的状态:

    • D: 无法中断的休眠状态 (通常 IO 的进程)
    • R: 正在执行中
    • S: 静止状态
    • T: 暂停执行
    • Z: 不存在但暂时无法消除
    • W: 没有足够的内存分页可分配
    • <: 高优先序的行程
    • N: 低优先序的行程
    • L: 有记忆体分页分配并锁在内存内 (实时系统或捱A I/O)
  • START: 进程开始时间,小于24小时(HH:MM),大于24小时(Mmm:SS),Mmm: 月份的前三个字母

  • TIME: 进程CPU运行时间,MMM:SS(分钟:秒)

  • COMMAND: 执行命令

查看进程的启动时间和运行时间
lstart      STARTED   time the command started.  See also bsdstart, start, start_time, and stime.
etime       ELAPSED   elapsed time since the process was started, in the form [[DD-]hh:]mm:ss
[root@centos35 ~]# ps -eo pid,lstart,etime,cmd | grep nginx
 1426 Mon May 22 09:52:32 2023 30-06:39:13 nginx: master process /usr/sbin/nginx
 1427 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
 1428 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
 1429 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
 1430 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
 1431 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
 1432 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
 1433 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
 1434 Mon May 22 09:52:32 2023 30-06:39:13 nginx: worker process
17519 Wed Jun 21 16:31:45 2023       00:00 grep --color=auto nginx
[root@centos35 ~]#
排序sort

使用sort命令排序

ps -aux | sort -nk 3

使用--sort排序,+正序,-逆序,-k排序参数

ps jax --sort=uid,-ppid,+pid

排序参数

KEY   LONG         描述
c     cmd          命令名称
C     pcpu         cpu使用率
f     flags        flags
g     pgrp         进程组ID
G     tpgid        tty组ID
j     cutime       累计用户时间
J     cstime       累计系统时间
k     utime        用户时间
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         内存大小(k)
S     share        amount of shared pages
t     tty          终端的次要装置号码
T     start_time   进程启动时间
U     uid          用户ID
u     user         用户名称
v     vsize        内存大小
y     priority     内核调度权限