如何在 python 中检索进程启动时间(或正常运行时间)
如何在 Linux 中的 python 中检索进程启动时间(或正常运行时间)?
我只知道,我可以调用“ps -p my_process_id -f”,然后解析输出。但这并不酷。
How to retrieve the process start time (or uptime) in python in Linux?
I only know, I can call "ps -p my_process_id -f" and then parse the output. But it is not cool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
通过使用 psutil https://github.com/giampaolo/psutil:
...加上它是跨平台的,不仅仅是Linux。
注意:我是这个项目的作者之一。
By using psutil https://github.com/giampaolo/psutil:
...plus it's cross platform, not only Linux.
NB: I am one of the authors of this project.
如果您是在要测量的 python 程序中执行此操作,则可以执行以下操作:
否则,您别无选择,只能解析
ps
或进入/proc/ pid。获取经过时间的一个很好的
bash
y 方法是:这只会以以下格式打印经过的时间,因此它应该很容易解析:(
如果它运行了不到一天,它只是
HH:MM:SS
)开始时间如下所示:
不幸的是,如果您的流程今天没有开始,这只会为您提供该日期是开始,而不是时间。
最好的方法是获取经过的时间和当前时间,然后做一些数学计算。以下是一个 python 脚本,它采用 PID 作为参数,并为您执行上述操作,打印出进程的开始日期和时间:
If you are doing it from within the python program you're trying to measure, you could do something like this:
Otherwise, you have no choice but to parse
ps
or go into/proc/pid
. A nicebash
y way of getting the elapsed time is:This will only print the elapsed time in the following format, so it should be quite easy to parse:
(if it's been running for less than a day, it's just
HH:MM:SS
)The start time is available like this:
Unfortunately, if your process didn't start today, this will only give you the date that it started, rather than the time.
The best way of doing this is to get the elapsed time and the current time and just do a bit of math. The following is a python script that takes a PID as an argument and does the above for you, printing out the start date and time of the process:
man proc
表示/ 中的第 22 项proc/my_process_id/stat
是:您现在的问题是,如何确定 jiffy 的长度以及如何确定系统何时启动。
后者的答案仍然来自
man proc
:它在/proc/stat
中,在它自己的一行中,如下所示:这是自纪元以来以秒为单位的测量。
前者的答案我不确定。
man 7 time
说:我们需要找到
HZ
,但我不知道如何从 Python 中实现这一点,除了希望该值为 250(正如维基百科声称是默认值)。ps
是这样获得的:这听起来像是 Python 的一个非常小的 C 模块做得很好的工作:)
man proc
says that the 22nd item in/proc/my_process_id/stat
is:Your problem now is, how to determine the length of a jiffy and how to determine when the system booted.
The answer for the latter comes still from
man proc
: it's in/proc/stat
, on a line of its own like this:That's a measurement in seconds since Epoch.
The answer for the former I'm not sure about.
man 7 time
says:We need to find
HZ
, but I have no idea on how I'd go about that from Python except for hoping the value is 250 (as Wikipedia claims is the default).ps
obtains it thus:This sounds like a job well done by a very small C module for Python :)
这是基于 badp 答案的代码:
但我不确定它是否正确。我编写了一个测试程序,该程序调用 sleep(5),然后运行它,但输出是错误的,并且每次运行的输出都会在几秒钟内变化。这是在 vmware 工作站 vm 中:
输出是:
Here's code based on badp's answer:
I'm not sure if it's right though. I wrote a test program that calls sleep(5) and then runs it and the output is wrong and varies over a couple of seconds from run to run. This is in a vmware workstation vm:
The output is:
此实现基于 Dan Benamy 的回答,并通过使用自纪元以来的正常运行时间(以秒为单位)而不是启动时间(以秒为单位)进行简化。该方法已使用 Python 2 和 3 进行了测试。
输出:
附录:您不能期望此代码具有毫秒精度。除了
/proc
接口提供的时间戳分辨率有限之外,执行总是有可能被任务切换中断。然而,它可能可以通过将代码拆分为首先读取原始数据,然后进行转换和计算来改进,但代价是代码不那么简洁,行数更多。This implementation is based on Dan Benamy's answer and simplified by using the uptime in seconds instead of the boot time in seconds since the epoch. Approach was tested with both Python 2 and 3.
Output:
Addendum: You can't expect millisecond accuracy from this code. Apart from the limited resolution of the timestamps provided by the
/proc
interfaces, there's always a possibility that the execution is interrupted by task switches. However it can probably be improved by splitting the code into reading the raw data first, then doing the conversions and calculations, to the expense of less concise code with more lines.你可以解析
/proc/uptime
对于 Windows 机器,你可以使用
you can parse
/proc/uptime
for windows machines, you can probably use wmi