简体   繁体   English

在 C 中检索上次启动时间戳

[英]Retrieving Last Boot Time Stamp In C

I've been able to retrieve the uptime value from /proc/uptime (which is in seconds).我已经能够从/proc/uptime (以秒为单位)检索正常运行时间值。 However, I need to retrieve the last boot time stamp using C. (I cannot use system(...) function to call uptime.)但是,我需要使用 C 检索上次启动时间戳。(我不能使用system(...)函数来调用正常运行时间。)

For example, when I run the uptime command, the value I get is:例如,当我运行uptime命令时,我得到的值是:

15:31:35 up 2 days,  4:14,  3 users,  load average: 0.04, 0.05, 0.05

I need that first part: 15:31:35 .我需要第一部分: 15:31:35 Is there a built-in C function that can get me this?有没有内置的 C 函数可以让我得到这个?

Open /proc/uptime and read it.打开/proc/uptime并阅读它。 The first number is the uptime in seconds.第一个数字是以秒为单位的正常运行时间。

Reading /proc/uptime is good.阅读 /proc/uptime 很好。

Or you can use FILE *fp = popen("uptime", "r") and read from fp the string you want.或者您可以使用FILE *fp = popen("uptime", "r")并从 fp 读取您想要的字符串。

That's a little more portable.这样便携一点。 ( By that I mean different unix's like MacOS ). (我的意思是像 MacOS 这样的不同的 unix )。

Oh, I just saw your lack of "system" requirement.哦,我刚刚看到您缺乏“系统”要求。 Never mind.没关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM