菜鸟笔记
提升您的技术认知

linux ps 命令查看进程启动及运行时间-ag真人游戏

同事问我怎样看一个进程的启动时间和运行时间,我第一反应当然是说用 ps 命令啦。
ps auxps -ef不就可以看时间吗?

我们来重新复习下ps aux的选项,这是类 bsd 风格的命令选项,因为不带“-”。

通过 man 可以看到 aux 选项解释如下:

a      lift the bsd-style "only yourself" 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 with a terminal (tty), 
or to list all processes when used together with the x option.
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      display user-oriented format.
              

然后再来看下ps aux的输出结果,其首行如下,说明了输出的各列:

user       pid %cpu %mem    vsz   rss tty      stat start   time command

我们可以看到start和time列,通过 man 其说明如下:

bsdstart    start     time the command started.  
if the process was started less than 24 hours ago, the output format is " hh:mm", 
else it is " mmm:ss" (where mmm is the three letters of the month). 
see also lstart, start, start_time, and stime.
bsdtime     time      accumulated cpu time, user   system.  
the display format is usually "mmm:ss", 
but can be shifted to the right if the process used more than 999 minutes of cpu time.

start 是命令启动的时间,如果在 24 小时之内启动的,则输出格式为”hh:mm”(小时:分钟),
否则就是”mmm:ss”(月份英语单词前 3 个字母:一月的第几号?[ss 这里面怎么理解?为什么有冒号呢?输出并没冒号]) 可以知道,这里并不能直接看出 24 小时之前启动的命令的精确启动时间。

time 是累积的 cpu 时间(user system),显示格式通常是”mmm:ss”。(分钟:秒) 可以看出,这里并不是指从命令启动开始到现在所花的时间。

带一个“-”为 unix 风格的命令选项。

-e     select all processes.  identical to -a.
-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.
-o format     user-defined format.  
format is a single argument in the form of a blank-separated or comma-separated list, 
which offers a way to specify individual output columns.  
the recognized keywords are described in the standard format specifiers section below.  
headers may be renamed (ps -o pid,ruser=realuser -o comm=command) as desired.  
if all column headers are empty (ps -o pid= -o comm=) then the header line will not be output.  
column width will increase as needed for wide headers; 
this may be used to widen up columns such as wchan (ps -o pid,wchan=wide-wchan-column -o comm).  
explicit width control (ps opid,wchan:42,cmd) is offered too.  
the behavior of ps -o pid=x,comm=y varies with personality; 
output may be one column named "x,comm=y" or two columnsnamed "x" and "y".  
use multiple -o options when in doubt.  
use the ps_format environment variable to specify a default as desired; 
defsysv and defbsd are macros that may be used to choose the default unix or bsd columns.              

然后再来看下ps -ef的输出结果,其首行如下,说明了输出的各列:

uid        pid  ppid  c stime tty          time cmd

我们可以看到 stim e和 time 列,通过 man 其说明如下 (我这台服务器上 ps 版本为 procps-ng version 3.3.9,man 中找不到 stime 的解释,通过观察输出,
我们可以推断这个 stime 其实和前面 start 是一样的,指的是命令启动的时间,这里有这个说明):

time 列也和前面说的 time 列一样指的命令使用的累积 cpu 时间。

回到引言中的问题,同事实际要问的是一个进程启动的精确时间和进程启动后所流逝的时间。
其实这 2 个时间也是可以通过 ps 命令输出的。 标识符如下:

       lstart      started   time the command started.  see also bsdstart, start, start_time, and stime.
       etime       elapsed   elapsed time since the process was started, in the form [[dd-]hh:]mm:ss.

例子:

查看 nginx 进程启动的精确时间和启动后所流逝的时间:

[root@iz25p102vo3z ~]# ps -eo pid,lstart,etime,cmd | grep nginx
16968 fri mar  4 16:04:27 2016 41-21:14:04 nginx: master process /usr/sbin/nginx
17826 fri mar  4 22:53:51 2016 41-14:24:40 nginx: worker process
18312 fri apr 15 13:18:31 2016       00:00 grep --color=auto nginx

 

网站地图