Linux command ps

本文详细介绍了 Linux 下的 ps 命令及其多种使用方法。通过不同的选项组合,如 -a, -u, -x, -e, -F, -C 和 -p 等,展示如何查看系统中进程的状态、按程序名称或 PID 查找进程、对进程信息进行排序等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ps(process): report a snapshot of the current processes

Linux 中运行的程序(包括守护进程,也就是后台进程)就是一个个带有状态的进程, ps呈现进程的数据快照

  • -a: 显示所有进程,除了session leaders(pid=sid)和未关联终端的进程
  • u: Display user-oriented format 用户向格式化
  • x: BSD风格:必须有一个终端,tty列
ps -aux

sg@ubuntu:~/temp$ ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
sg     39292  0.0  0.0  27876  1508 pts/1    R+   07:12   0:00 ps ux
sg     52640  0.0  0.0  54424  6252 ?        Ss   05:23   0:00 /lib/systemd/systemd --user
sg     52646  0.0  0.0 171364  2388 ?        S    05:23   0:00 (sd-pam)
sg     52653  0.0  0.0  34552  3496 ?        Ss   05:23   0:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation
sg     52681  0.0  0.0 187424  4872 ?        Sl   05:23   0:00 /usr/lib/dconf/dconf-service
sg     52684  0.0  0.0 282208  6516 ?        Ssl  05:23   0:00 /usr/lib/gvfs/gvfsd
sg     52689  0.0  0.0 350208  5604 ?        Sl   05:23   0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
sg     52825  0.0  0.0 105988  4228 ?        S    05:23   0:00 sshd: sg@pts/1
sg     52833  0.0  0.0  13872  6052 pts/1    Ss   05:23   0:00 -bash

...
  • -e: 显示所有进程,真正所有的进程
  • -F: 显示所有列
ps -eF

sg@ubuntu:~/temp$ ps -eF
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0 34387  7380   0 Apr18 ?        00:08:01 /sbin/init
root         2     0  0     0     0   1 Apr18 ?        00:00:00 [kthreadd]
root         3     2  0     0     0   0 Apr18 ?        00:00:56 [ksoftirqd/0]
root         5     2  0     0     0   0 Apr18 ?        00:00:00 [kworker/0:0H]
root         7     2  0     0     0   1 Apr18 ?        00:13:52 [rcu_sched]
root         8     2  0     0     0   1 Apr18 ?        00:00:00 [rcu_bh]

... ...
  • -C 按程序名称查找
  • -p 按process ID 查找
ps -C bash
ps -p 52833

sg@ubuntu:~/temp$ ps -C bash
  PID TTY          TIME CMD
52833 pts/1    00:00:00 bash
sg@ubuntu:~/temp$ ps -p 52833
  PID TTY          TIME CMD
52833 pts/1    00:00:00 bash
  • –sort : 排序, 可以多列排序, 默认为正序(+),
ps ux --sort=uid,-ppid,+pid

sg@ubuntu:~/temp$ ps ux --sort=uid,-ppid,+pid
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
sg     39615  0.0  0.0  27876  1476 pts/1    R+   07:34   0:00 ps ux --sort=uid,-ppid,+pid
sg     52833  0.0  0.0  13872  6052 pts/1    Ss   05:22   0:00 -bash
sg     52646  0.0  0.0 171364  2388 ?        S    05:22   0:00 (sd-pam)
sg     52653  0.0  0.0  34552  3496 ?        Ss   05:22   0:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation
sg     52681  0.0  0.0 187424  4872 ?        Sl   05:22   0:00 /usr/lib/dconf/dconf-service
sg     52684  0.0  0.0 282208  6516 ?        Ssl  05:22   0:00 /usr/lib/gvfs/gvfsd
sg     52689  0.0  0.0 350208  5604 ?        Sl   05:22   0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
sg     52825  0.0  0.0 105988  4228 ?        S    05:22   0:00 sshd: sg@pts/1
sg     52640  0.0  0.0  54424  6252 ?        Ss   05:22   0:00 /lib/systemd/systemd --user
  • 与grep连用: 查询含有session的行
  • head -1; : 取首行,也就是标题行
  • |: 管道命令 把ps进程查询结果传递给grep命令进行过滤
  • grep -w: word-regexp 查询包含整个单词的行
ps ux | head -1;ps ux | grep -w session

sg@ubuntu:~/temp$ ps ux | head -1;ps ux | grep -w session
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
sg     39952  0.0  0.0  12944   936 pts/1    S+   07:42   0:00 grep --color=auto -w session
sg     52653  0.0  0.0  34552  3496 ?        Ss   05:22   0:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值