引言

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

ps aux选项及输出说明

我们来重新复习下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”。(分钟:秒) 可以看出,这里并不是指从命令启动开始到现在所花的时间。

ps -ef选项及输出说明

带一个“-”为 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 时间。

使用 ps 命令查看进程启动的精确时间和启动后所流逝的时间

回到引言中的问题,同事实际要问的是一个进程启动的精确时间和进程启动后所流逝的时间。
其实这 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

Linux ps 命令查看进程启动及运行时间的更多相关文章

  1. 查看程序是否启动或者关闭--比如查看Tomcat是否开启!直接用ps命令查看进程就行了啊

    1.查看程序是否启动或者关闭--比如查看Tomcat是否开启!直接用ps命令查看进程就行了啊 2.Tomcat服务器和虚拟机的关系,Tomcat启动运行过程要调用系统环境变量的java_home啊,J ...

  2. ps命令查看进程指定项目信息、用户名过长显示UID

    有次一个在使用ps命令时,发现部分用户显示的是用户名,有些用户显示的是UID,那是因为用户名长度超过8位的:也就是说ps命令用户名列默认只能显示8位(含8位)的用户名,超过8位就显示UID,如何让长度 ...

  3. 00014 - linux中用top、ps命令查看进程中的线程

    在Linux上显示某个进程的线程的几种方式. 方法一:PS 在ps命令中,“-T”选项可以开启线程查看.下面的命令列出了由进程号为<pid>的进程创建的所有线程. 1.$ ps -T -p ...

  4. Linux常用命令 查看进程信息时 copy的-----温故而知新

    1.查进程    ps命令查找与进程相关的PID号:    ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程序.    ps c 列出程序时,显示每个程序真正的 ...

  5. Linux基础命令——查看进程命令

    linux是一个 多进程   多用户的操作系统 ps(显示当前进程的状态) ps -ef  查看当前linux 进程 ps -ef | grep 'mysqld'  过滤mysql的进程 (grep  ...

  6. linux ps 命令查看进程状态

    显示其他用户启动的进程(a) 查看系统中属于自己的进程(x) 启动这个进程的用户和它启动的时间(u) 使用“date -s”命令来修改系统时间 比如将系统时间设定成1996年6月10日的命令如下. # ...

  7. linux ps命令查看最消耗CPU、内存的进程

    1.CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 2.内存消耗最多的前10个进程 ps auxw|head -1;ps a ...

  8. Android中使用ps命令查看进程PID

    adb shell "ps | grep com.sina.weibo" 这个命令可以看到微博的应用线程信息. PID:进程号 PPID:父进程号 VSIZE:进程的虚拟内存大小 ...

  9. linux ps命令,查看进程cpu和内存占用率排序(转)

    使用以下命令查看: ps -aux | sort -k4,4n ps auxw --sort=rss ps auxw --sort=%cpu linux 下的ps命令 %CPU 进程的cpu占用率 % ...

随机推荐

  1. Canvas与Image互相转换示例以及利用该技术实现微信长按自动识别二维码功能

    现在扫描二维码已经很普遍,微信扫一扫即可,但是如果二维码是在自己的手机上呢?那就要用到微信里的一个功能了,手指长按二维码,会弹出自动识别的选项,点确定就可以看到二维码的内容了.那么怎么通过前端实现这个 ...

  2. inline-block元素垂直对齐

    多个inline-block元素使用vertical-align:middle无法对齐,必须有个height:100%的子元素才行,通常使用伪元素.另一种方法是添加line-height:normal ...

  3. 负载平衡(cogs 741)

    «问题描述:G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运.«编程任务:对于给定的n 个环 ...

  4. linux把时间类型值转换为数值型

    字符及字符串处理函数:字符及字符串处理函数的处理对象均为字符型数据,但其返回值类型各异.1.取子串函数:格式:substr(c,n1,n2)功能:取字符串C第n1个字符起的n2个字符.返回值类型是字符 ...

  5. BZOJ【1625】宝石手镯

    1625: [Usaco2007 Dec]宝石手镯 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1007  Solved: 684[Submit][St ...

  6. luogu 2463 [SDOI2008]Sandy的卡片 kmp || 后缀数组 n个串的最长公共子串

    题目链接 Description 给出\(n\)个序列.找出这\(n\)个序列的最长相同子串. 在这里,相同定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串. 思路 参考:hzwe ...

  7. php --图片加图片水印

    最近在做一个视频网站需要视频有一个封面图片,但是不能是普通的图片,能让别人一眼看出来是 视频,所以我就在图片上面加了视频播放器的那种水印,具体代码如下: <?php/** * 图片加水印(适用于 ...

  8. 了解Binder机制原理和底层实现

    参考:http://www.2cto.com/kf/201606/515548.html 1.Binder通信机制介绍 这篇文章会先对比Binder机制与Linux的通信机制的差别,了解为什么Andr ...

  9. Network | TCP

    Transmission Control Protocol, TCP是一种面向连接的.可靠的.基于字节流的传输层通信协议. 应用层向TCP层发送用于网间传输的.用8位字节表示的数据流,然后TCP把数据 ...

  10. bzoj 2889: Tree Conundrum

    2889: Tree Conundrum Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 37[Submit][Status][ ...