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