gpt4 book ai didi

unix - 只需几秒钟即可获得 SunOS UNIX 机器的正常运行时间

转载 作者:行者123 更新时间:2023-12-04 10:32:27 25 4
gpt4 key购买 nike

如何仅以秒为单位确定 SunOS UNIX 机器上的正常运行时间?

在 Linux 上,我可以简单地 cat/proc/uptime 并采用第一个参数:

cat /proc/uptime | awk '{print $1}'

我试图在 SunOS UNIX 机器上做同样的事情,但没有/proc/uptime。
有一个 uptime 命令显示以下输出:
$ uptime
12:13pm up 227 day(s), 15:14, 1 user, load average: 0.05, 0.05, 0.05

我真的不想编写代码来将日期转换为秒数,而且我确定之前一定有人有过这个要求,但我一直无法在互联网上找到任何东西。

谁能告诉我如何在几秒钟内获得正常运行时间?

TIA

最佳答案

如果你不介意编译一个小的 C 程序,你可以使用:

#include <stdio.h>
#include <string.h>
#include <utmpx.h>

int main()
{
int nBootTime = 0;
int nCurrentTime = time ( NULL );
struct utmpx * ent;

while ( ( ent = getutxent ( ) ) ) {
if ( !strcmp ( "system boot", ent->ut_line ) ) {
nBootTime = ent->ut_tv.tv_sec;
}
}

printf ( "System was booted %d seconds ago\n", nCurrentTime - nBootTime );
endutxent();

return 0;
}

来源: http://xaxxon.slackworks.com/rsapi/

关于unix - 只需几秒钟即可获得 SunOS UNIX 机器的正常运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1661006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com