引言

同事问我怎样看一个进程的启动时间和运行时间,我第一反应当然是说用 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. ofbiz数据库表结构设计(1)- PARTY

    ofbiz的精华就在于其数据结构(表结构)的设计.数据结构的通用性也决定了ofbiz几乎可以适用任何企业应用.我们首先来看看PARTY相关的表结构设计. 在ofbiz中,PARTY是个抽象概念,它可以 ...

  2. 【HDOJ5559】Frog and String(构造)

    题意:给定n,m,k,要求构造出一个长度为n,最多使用前k个大写字母,有m个不同回文子串的字符串 1<=n,m<=1e5,1<=k<=26 思路:打表找规律 本质上是要找到不让 ...

  3. .Net Framework 4.0: Using System.Lazy<T>

    原文发布时间为:2011-04-26 -- 来源于本人的百度文章 [由搬家工具导入] http://weblogs.asp.net/gunnarpeipman/archive/2009/05/19/n ...

  4. c++学习重点分析

     C++是一种语言,仅仅是它的语法.特性.标准类库就已经是一门非常高深的课程,所以在开始学习的时候,必须先要打好基础.要知道当我们在学习它的时候重点应该注意什么. 一.#include “filena ...

  5. 在android app中使用STL库(转)

    1.在jni目录下新建Application.mk; 加入 APP_STL := stlport_static右边的值还可以换成下面几个: system - 使用默认最小的C++运行库,这样生成的应用 ...

  6. wxformbuilder

    1.打开wxFormBuilder,按开始一个空项目.您也可以执行File|New来创建新项目2.从Object Properties(对象属性)面板配置项目的设置A.选择产生什么类型的代码. 现在你 ...

  7. python 代码格式

    python 代码格式 Python对代码的缩进要求非常严格,如果不采用合理的代码缩进,将抛出SyntaxError异常 Python语句中一般以新行作为为语句的结束符.但是我们可以使用斜杠( )将一 ...

  8. 记录: 一次解决整型溢出攻击(使用scala,隐式转换)

    最近项目遇到一次整型溢出攻击 有一个功能,玩家购买num个物品. 每个物品花费14货币. 客户端限制玩家只能购买 1-9999个该物品. 但是某玩家通过技术手段,获得了客户端的运行权限. 于是发送协议 ...

  9. 关于webpack的cdn配置

    在webpack中活用cdn可以大幅度减少打包文件的体积,配置方法也非常简单. 首先现在入口html文件(index.html)里引入相应cdn连接,然后配置webpack: module.expor ...

  10. dubbo框架的简单介绍

    以下的官网的介绍. dubbo是SOA.小例子是简单的远程调用(生产者消费者的模式出现).http://blog.csdn.net/huangyekan/article/details/4217267 ...