引言

同事问我怎样看一个进程的启动时间和运行时间,我第一反应当然是说用 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. 字符串函数 (strfun)

    字符串函数 (strfun) 题目描述 两个等长的由大写英文字母构成的字符串a和b,从a中选择连续子串x,从b中选出连续子串y.子串x与子串y的长度相等. 定义函数f(x,y)为满足条件xi=yi(1 ...

  2. vue基础教程

    1.执行npm install 2.安装stylus,(npm install之后node_module已经有了stylus,但还是要再安装一次) npm install --save-dev sty ...

  3. nginx问题

    1.杀死nginx之后,找不到pid http://bbs.pxecn.com/thread-122116-1-1.html

  4. react之只用classNames避免字符串拼接

    之前在react当中使用了字符串拼接的方式来拼接类名的字符串,这种方法不仅不够方便,还会出现很多问题 使用classNames这个工具,可以省去拼接字符串的烦恼,大大提高开发效率 首先,最简单的使用方 ...

  5. Tortoise SVN 提示 clean up 然而 clean up 没卵用的解决方法

    当 svn 提示clean up时,而clean up 仍然没用的情况下 先下载 sqlite3.exe 然后 在svn目录下,.svn 隐藏文件夹中 有个 wc.db 的文件, 执行下面命令行  s ...

  6. ofbiz数据库表结构设计(1)- PARTY

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

  7. (二) Spring项目的搭建

    传统的项目搭建,是将所依赖的第三方jar包复制到项目的类路径下.但是,这样带来的问题是,无法更好的对这些jar包进行动态管理. 目前主流的构建工具有:Ant.Maven.Gradle.以Maven为例 ...

  8. Javascript&Html-history对象

    Javascript&Html-history对象 history对象保存着用户的上网记录,这些记录从用户打开浏览器开始. 用户借助history对象实现的跳转. history.go(-1) ...

  9. XPath gramma

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. XML 实例文档 我们将在下面的例子中使用这个 XML 文档. & ...

  10. poj 2280 Islands and Bridges 哈密尔顿路 状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求一条哈密尔顿路径\(C_1C_2...C_n\),使其\(value\)最大. \(value\)的计算方式如下:\[\begin{aligned}v ...