0%

ps命令

一、常用用法

有两种常用用法:

1
ps -o pid -o ppid -o start_time -o s -o tname -o args --sort=start_time,pid -e
1
ps -o pid -o ppid -o start_time -o s -o ruser -o rgroup -o euser -o egroup -o tname -o args -p PID

二、含义与选项

2.1、含义

1、第一种用法的含义
查询所有的进程,需要显示的列有“pid,ppid,start_time,s,tname,args”,先根据“start_time”列升序排序,再根据“pid”列升序排序。
2、第二种用法的含义
根据进程ID(即PID)查询某个进程的详情,需要显示的列有“pid,ppid,start_time,s,ruser,rgroup,euser,egroup,tname,args”。

2.2、选项表示的意思

“-o 列特征码”:表示在最终的结果中显示“列特征码”对应的列。
“–sort 列特征码组合”:表示根据列特征码组合,对最终显示结果进行排序。
“-e”:表示列出所有进程。
“-p PID”:表示只列出进程ID为“PID”的进程。

三、其他

3.1、使用“ps”命令的目标与选择

3.1.1、期望的关于“ps”命令的目标

能够列出所有的进程,只列出自己所选定的几列的信息,能够根据进程创建时间和进程ID升序排序,能够查询已知进程ID对应进程的详细情况。

3.1.2、具体选择

“ps”命令有好几套实现,在具体选择的过程中,要注意使用标准实现方案。
关于“列出所有进程”的标准做法为:ps -e
关于“只列出自己所选定的几列的信息”的标准做法:[-o keyword]+(表示1到多个这样的组合,其中“keyword”来自于“man ps”页的“STANDARD FORMAT SPECIFIERS”小节)
关于“能够根据进程创建时间和进程ID升序排序”的标准做法:--sort=keyword(“keyword”跟上面一样,同样来自于“man ps”页的“STANDARD FORMAT SPECIFIERS”小节),如果需要先根据进程创建时间,再根据进程ID升序排序,那么就是“–sort=start_time,pid”。
关于“能够查询已知进程ID对应进程的详细情况”的标准做法:-p PID

3.2、另外一些概念介绍

3.2.1、什么是“session leaders”

In Linux, every process has several IDs associated with it, including:

  1. Process ID (PID)
    This is an arbitrary number identifying the process. Every process has a unique ID, but after the process exits and the parent process has retrieved the exit status, the process ID is freed to be reused by a new process.
  2. Parent Process ID (PPID)
    This is just the PID of the process that started the process in question.
  3. Process Group ID (PGID)
    This is just the PID of the process group leader. If PID == PGID, then this process is a process group leader.
  4. Session ID (SID)
    This is just the PID of the session leader. If PID == SID, then this process is a session leader.

Sessions and process groups are just ways to treat a number of related processes as a unit. All the members of a process group always belong to the same session, but a session may have multiple process groups.
Normally, a shell will be a session leader, and every pipeline executed by that shell will be a process group. This is to make it easy to kill the children of a shell when it exits. (See exit(3) for the gory details.)
I don’t think there is a special term for a member of a session or process group that isn’t the leader.

3.2.2、进程的“cpu”列信息的含义

cpu utilization of the process in “##.#” format.Currently, it is the CPU time used divided by the time the process has been running(cputime/realtime ratio), expressed as a percentage. It will not add up to 100% unless you are lucky.

3.2.3、进程状态指示符表示的进程状态

Here are the different values that the s output specifiers (header “S”) will display to describe the state of a process:

进程状态码 含义
D uninterruptible sleep (usually IO)
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped, either by a job control signal or because it is being traced
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct (“zombie”) process, terminated but not reaped by its parent

参考文献

[1]man ps
[2]http://unix.stackexchange.com/questions/18166/what-are-session-leaders-in-ps

您的支持将鼓励我继续分享!