ps更全面的介绍见官网。一个简单的常见命令,竟也这么复杂,要学的内容很多。用的时候查查字典。
几点感触:1) ps三种风格参数选项;2) note中ps的原理,数据从/proc中读取数据;3) note中僵尸进程;4) 根据需要不同场景下的ps命令。
ps - report a snapshot of the current processes.
ps 仅仅显示查询当前活动进程的相关信息。如果要重复查询进程动态变化的相关信息,请使用TOP命令。
ps命令的3种参数选项:
1 UNIX options 可以分组,且参数选项前面必须要有一个破折号。
2 BSD options 可以分组,但参数选项前面不能有一个破折号。
3 GNU long options参数选项前面必须要有两个破折号。
注意:ps -aux和ps aux的区别。The POSIX and UNIX标准要求ps -aux通过-a参数来打印出所有进程信息和-x参数打印其用户叫x的进程的信息。假如x用户不存在,会打印警告信息。其旨在帮助转换旧脚本和习惯。它很脆弱,可能会发生变化,因此不应依赖它。
默认情况下,PS选择将相同有效用户ID(euid =EUID)作为当前用户并与调用者相关联的所有进程。PS展示了进程id(pid=PID),进程相关的终端(tname=TTY),累积起来的CPU占用时间(time=TIME),以及可执行文件名(ucmd=CMD)。默认情况下,PS输出不排序。
BSD风格的参数选项,默认情况下将增加字段显示:进程状态 (stat=STAT),显示命令参数(args=COMMAND)而不是可执行文件名。You can override this with the PS_FORMAT environment variable.
example
使用标准格式查看系统上的所有进程:ps -e、ps -ef、 ps -eF、 ps -ely
使用BSD格式查看系统上的每一个进程:ps ax、ps axu
打印进程树: ps -ejH、 ps axjf
打印线程相关信息: ps -eLf、ps axms
打印安全(用户相关)信息:ps -eo euser,ruser,suser,fuser,f,comm,label、 ps axZ、 ps -eM
打印以root用户运行的所有进程: ps -U root -u root u
打印用户自定义格式下的所有进程: 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
打印syslogd名字有关的进程PID:ps -C syslogd -o pid=, -C cmdlist
打印PID为42有关的进程名字: ps -q 42 -o comm=
note
This ps works by reading the virtual files in /proc. This ps does not need to be setuid kmem or have any privileges to run.Do not give this ps any special permissions.
CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%.
The SIZE and RSS fields don't count some parts of a process including the page tables, kernel stack, struct thread_info, and struct task_struct. This is usually at least 20 KiB of memory that is always resident. SIZE is the virtual size of the process (code+data+stack).
Processes marked <defunct> are dead processes (so-called "zombies") that remain because their parent has not destroyed them properly. These processes will be destroyed by init(8) if the parent process exits.
If the length of the username is greater than the length of the display column, the username will be truncated. See the -o and -O formatting options to customize length.
Commands options such as ps -aux are not recommended as it is a confusion of two different standards. According to the POSIX and UNIX standards, the above command asks to display all processes with a TTY (generally the commands users are running) plus all processes owned by a user named x. If that user doesn't exist, then ps will assume you really meant ps aux.
网友评论