如何获取长时间运行的 Linux 进程的启动时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5731234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:44:40  来源:igfitidea点击:

How to get the start time of a long-running Linux process?

linuxbashprocess

提问by ajwood

Is it possible to get the start time of an old running process? It seems that pswill report the date (not the time) if it wasn't started today, and only the year if it wasn't started this year. Is the precision lost forever for old processes?

是否可以获取旧运行进程的开始时间?ps如果今天没有开始,它似乎会报告日期(而不是时间),如果今年没有开始,则只会报告年份。旧流程是否会永远失去精度?

采纳答案by wkl

You can specify a formatter and use lstart, like this command:

你可以指定一个格式化程序并使用lstart,就像这个命令:

ps -eo pid,lstart,cmd

The above command will output all processes, with formatters to get PID, command run, and date+time started.

上面的命令将输出所有进程,带有格式化程序以获取 PID、命令运行和开始日期+时间。

Example (from Debian/Jessie command line)

示例(来自 Debian/Jessie 命令行)

$ ps -eo pid,lstart,cmd
  PID CMD                                          STARTED
    1 Tue Jun  7 01:29:38 2016 /sbin/init                  
    2 Tue Jun  7 01:29:38 2016 [kthreadd]                  
    3 Tue Jun  7 01:29:38 2016 [ksoftirqd/0]               
    5 Tue Jun  7 01:29:38 2016 [kworker/0:0H]              
    7 Tue Jun  7 01:29:38 2016 [rcu_sched]                 
    8 Tue Jun  7 01:29:38 2016 [rcu_bh]                    
    9 Tue Jun  7 01:29:38 2016 [migration/0]               
   10 Tue Jun  7 01:29:38 2016 [kdevtmpfs]                 
   11 Tue Jun  7 01:29:38 2016 [netns]                     
  277 Tue Jun  7 01:29:38 2016 [writeback]                 
  279 Tue Jun  7 01:29:38 2016 [crypto]                    
      ...

You can read ps's manpage or check Opengroup's pagefor the other formatters.

您可以阅读ps的联机帮助页或检查 Opengroup 的页面以了解其他格式化程序。

回答by Adam Matan

ls -ltrh /proc | grep YOUR-PID-HERE

For example, my Google Chrome's PID is 11583:

例如,我的谷歌浏览器的 PID 是 11583:

ls -l /proc | grep 11583
dr-xr-xr-x  7 adam       adam                     0 2011-04-20 16:34 11583

回答by bash-o-logist

 ps -eo pid,etime,cmd|sort -n -k2

回答by Nathan

The ps command (at least the procps version used by many Linux distributions) has a number of format fields that relate to the process start time, including lstartwhich always gives the full date and time the process started:

ps 命令(至少是许多 Linux 发行版使用的 procps 版本)具有许多与进程启动时间相关的格式字段,包括lstart始终提供进程启动的完整日期和时间:

# ps -p 1 -wo pid,lstart,cmd
  PID                  STARTED CMD
    1 Mon Dec 23 00:31:43 2013 /sbin/init

# ps -p 1 -p $$ -wo user,pid,%cpu,%mem,vsz,rss,tty,stat,lstart,cmd
USER       PID %CPU %MEM    VSZ   RSS TT       STAT                  STARTED CMD
root         1  0.0  0.1   2800  1152 ?        Ss   Mon Dec 23 00:31:44 2013 /sbin/init
root      5151  0.3  0.1   4732  1980 pts/2    S    Sat Mar  8 16:50:47 2014 bash

For a discussion of how the information is published in the /proc filesystem, see https://unix.stackexchange.com/questions/7870/how-to-check-how-long-a-process-has-been-running

有关如何在 /proc 文件系统中发布信息的讨论,请参阅 https://unix.stackexchange.com/questions/7870/how-to-check-how-long-a-process-has-been-running

(In my experience under Linux, the time stamp on the /proc/ directories seem to be related to a moment when the virtual directory was recently accessed rather than the start time of the processes:

(根据我在 Linux 下的经验,/proc/ 目录上的时间戳似乎与最近访问虚拟目录的时刻有关,而不是进程的启动时间:

# date; ls -ld /proc/1 /proc/$$ 
Sat Mar  8 17:14:21 EST 2014
dr-xr-xr-x 7 root root 0 2014-03-08 16:50 /proc/1
dr-xr-xr-x 7 root root 0 2014-03-08 16:51 /proc/5151

Note that in this case I ran a "ps -p 1" command at about 16:50, then spawned a new bash shell, then ran the "ps -p 1 -p $$" command within that shell shortly afterward....)

请注意,在这种情况下,我在大约 16:50 运行了“ps -p 1”命令,然后生成了一个新的 bash shell,然后不久之后在该 shell 中运行了“ps -p 1 -p $$”命令...... .)

回答by tripleee

As a follow-up to Adam Matan's answer, the /proc/<pid>directory's time stamp as such is not necessarily directly useful, but you can use

作为Adam Matan 回答的后续,/proc/<pid>目录的时间戳不一定直接有用,但您可以使用

awk -v RS=')' 'END{print }' /proc/12345/stat

to get the start time in clock ticks since system boot.1

获取自系统启动以来时钟滴答的开始时间。1

This is a slightly tricky unit to use; see also convert jiffies to secondsfor details.

这是一个使用起来有点棘手的单位;有关详细信息,另请参阅将 jiffies 转换为秒

awk -v ticks="$(getconf CLK_TCK)" 'NR==1 { now=; next }
    END { printf "%9.0f\n", now - (/ticks) }' /proc/uptime RS=')' /proc/12345/stat

This should give you seconds, which you can pass to strftime()to get a (human-readable, or otherwise) timestamp.

这应该给你几秒钟,你可以传递给它strftime()以获得(人类可读的,或其他)时间戳。

awk -v ticks="$(getconf CLK_TCK)" 'NR==1 { now=; next }
    END { print strftime("%c", systime() - (now-(/ticks))) }' /proc/uptime RS=')' /proc/12345/stat

Updated with some fixes from Stephane Chazelas in the comments; thanks as always!

更新了评论中 Stephane Chazelas 的一些修复;一如既往地感谢!

If you only have Mawk, maybe try

如果你只有 Mawk,不妨试试

awk -v ticks="$(getconf CLK_TCK)" -v epoch="$(date +%s)" '
  NR==1 { now=; next }
  END { printf "%9.0f\n", epoch - (now-(/ticks)) }' /proc/uptime RS=')' /proc/12345/stat |
xargs -i date -d @{}


1man proc; search for starttime.

1人 proc; 搜索开始时间

回答by Stackoverflow

    ps -eo pid,cmd,lstart | grep YOUR-PID-HERE