如何获取长时间运行的Linux进程的启动时间?
是否可以获得旧的正在运行的进程的开始时间?如果今天没有开始,ps
似乎会报告日期(而不是时间),如果不是今年开始,则仅报告年份。旧工艺的精度是否会永远丧失?
Is it possible to get the start time of an old running process? It seems that ps
will 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以指定格式化程序并使用
lstart
,例如以下命令:上面的命令将输出所有进程,并使用格式化程序来获取PID、命令运行和启动日期+时间。
示例(来自 Debian/Jessie 命令行)
您可以阅读
ps
的联机帮助页或 检查 Opengroup 的页面以了解其他格式化程序。You can specify a formatter and use
lstart
, like this command:The above command will output all processes, with formatters to get PID, command run, and date+time started.
Example (from Debian/Jessie command line)
You can read
ps
's manpage or check Opengroup's page for the other formatters.ps 命令(至少是许多 Linux 发行版使用的 procps 版本)具有许多与进程启动时间相关的格式字段,包括始终给出进程启动的完整日期和时间的
lstart
:有关如何在 /proc 文件系统中发布信息的讨论,请参阅
https://unix.stackexchange.com/ questions/7870/how-to-check-how-long-a-process-has-been-running
(根据我在 Linux 下的经验, /proc/ 目录上的时间戳似乎与某个时刻有关当最近访问虚拟目录而不是进程的启动时间时:
请注意,在这种情况下,我在大约 16:50 运行了“ps -p 1”命令,然后生成了一个新的 bash shell,然后运行了“ps -p 1”命令不久之后,该 shell 中的 p 1 -p $$" 命令......)
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
lstart
which always gives the full date and time the process started: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
(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:
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....)
作为 Adam Matan 的回答的后续内容,
/proc/
目录的时间戳本身不一定直接有用,但您可以使用它来获取自系统启动以来的时钟周期开始时间。1
这是一个使用起来有点棘手的单位;另请参阅将 jiffies 转换为秒了解详细信息。
这应该为您提供秒数,您可以将其传递给
strftime()
来获取(人类可读的或其他)时间戳。更新了评论中 Stephane Chazelas 的一些修复;一如既往的感谢!
如果你只有 Mawk,也许可以尝试
1 man过程;搜索开始时间。
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 useto get the start time in clock ticks since system boot.1
This is a slightly tricky unit to use; see also convert jiffies to seconds for details.
This should give you seconds, which you can pass to
strftime()
to get a (human-readable, or otherwise) timestamp.Updated with some fixes from Stephane Chazelas in the comments; thanks as always!
If you only have Mawk, maybe try
1 man proc; search for starttime.
例如,我的 Google Chrome 的 PID 是 11583:
For example, my Google Chrome's PID is 11583:
但我能在纪元秒内得到答案吗?
But can I get the answer in epoch seconds?
使用命令ls -ld /proc/process_id,其中可以使用top命令找到process_id
use the command ls -ld /proc/process_id where process_id can be find using top command