ps命令常用参数详解

发布时间:2020-01-11编辑:脚本学堂
本文介绍下,linux系统中ps命令的一些常用参数,有需要的朋友可以参考学习下。

本节内容:
linuxjishu/14079.html target=_blank class=infotextkey>ps命令常用参数

一直都用ps命令,但是很少去琢磨其中参数的含义,今天分享一些ps命令的常用参数,供大家参考。

我的常用命令:
 

复制代码 代码示例:
ps -def 
-d Select all processes except session leaders.

什么是session leader。
参考该解释:http://www.win.tue.nl/~aeb/linux/lk/lk-10.html#ss10.3
 

Every process group is in a unique session. (When the process is created, it becomes a member of the session of its parent.) By convention, the session ID of a session equals the process ID of the first member of the session, called the session leader. A process finds the ID of its session using the system call getsid(). 
 
Every session may have a controlling tty, that then also is called the controlling tty of each of its member processes. A file descriptor for the controlling tty is obtained by opening /dev/tty. (And when that fails, there was no controlling tty.) Given a file descriptor for the controlling tty, one may obtain the SID using tcgetsid(fd). 
 
A session is often set up by a login process. The terminal on which one is logged in then becomes the controlling tty of the session. All processes that are descendants of the login process will in general be members of the session. 

进程是按照进程组管理的,进程组又属于session。

关系如下:
每个session拥有一个或者多个进程组,每个进程组拥有一个或多个进程。
第一个属于某个session的进程id就是这个session的 leader, session id就用它的进程id。
和进程相关的id有几种,进程id, 父进程id, 进程组id 和 session id.
参考:http://unix.stackexchange.com/questions/18166/what-are-session-leaders-in-ps

-e 参数等同于 -A,
[plain] view plaincopyprint?
-e     Select all processes.  Identical to -A. 

-f 参数 显示完整格式
[plain] view plaincopyprint?
-f     Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns.  It also causes the command arguments to be 
              printed.  When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added.  See the c option, the format keyword args, and the 
              format keyword comm. 

不过还有个常用的方式:
ps axu
不同于ps -def 用标准风格,这是BSD风格(不用-作为引导参数), BSD风格会在另一篇博客中介绍。
-a 参数
-a     Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal. 
-x 参数 和tty有关,tty会在另一篇文章中解释
x      Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when 
              the ps personality setting is BSD-like.  The set of processes selected in this manner is in addition to the set of processes selected by other means.  An 
              alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the 
              a option. 
-u 参数
-u userlist 
              Select by effective user ID (EUID) or name.  This selects the processes whose effective user name or ID is in userlist. 
 
              The effective user ID describes the user whose file access permissions are used by the process (see geteuid(2)).  Identical to U and --user. 

还有一种,通过直接输入命令查找,用-C参数,比如:
 

复制代码 代码示例:
$ UNIX95= ps -C nginx 
  PID TTY          TIME CMD 
 1258 ?        00:00:00 nginx 
 1259 ?        00:00:00 nginx 
CHNshu6889@sloop2:~$ ps aux | grep nginx 
root      1258  0.0  0.0  31188  1028 ?        Ss   09:24   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf 
root      1259  0.0  0.0  47312  2708 ?        S    09:24   0:00 nginx: worker process                    
70780027  4336  0.0  0.0  13652   976 pts/1    S+   10:40   0:00 grep --color=auto nginx 

注意,前面需要输入UNIX95=。

相关阅读:linux命令实例教程,大家可以深入学习下常用的linux命令。