OSX:以编程方式获得正常运行时间?

发布于 2024-09-10 03:01:52 字数 106 浏览 6 评论 0原文

类似于 linux 的东西

cat /proc/uptime

,它返回以秒为单位的正常运行时间,并且最好不解析正常运行时间(1)。

Something similar to linux

cat /proc/uptime

which returns the uptime in seconds, and preferably not parsing uptime(1).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

静若繁花 2024-09-17 03:01:52

老问题,我知道,但我需要做同样的事情,所以我想我应该发布我正在使用的代码,这是我从 http://cocoadev.com/wiki/FindingUptime

#include <time.h>
#include <errno.h>
#include <sys/sysctl.h>

double uptime()
{
    struct timeval boottime;
    size_t len = sizeof(boottime);
    int mib[2] = { CTL_KERN, KERN_BOOTTIME };
    if( sysctl(mib, 2, &boottime, &len, NULL, 0) < 0 )
    {
        return -1.0;
    }
    time_t bsec = boottime.tv_sec, csec = time(NULL);

    return difftime(csec, bsec);
}

Old question, I know, but I needed to do the same thing so I thought I'd post the code I'm using, which I got from http://cocoadev.com/wiki/FindingUptime

#include <time.h>
#include <errno.h>
#include <sys/sysctl.h>

double uptime()
{
    struct timeval boottime;
    size_t len = sizeof(boottime);
    int mib[2] = { CTL_KERN, KERN_BOOTTIME };
    if( sysctl(mib, 2, &boottime, &len, NULL, 0) < 0 )
    {
        return -1.0;
    }
    time_t bsec = boottime.tv_sec, csec = time(NULL);

    return difftime(csec, bsec);
}
套路撩心 2024-09-17 03:01:52

Wikipedia 上的 Uptime 文章 有一个有趣的线索:

使用 sysctl

还有一种使用sysctl的方法
调用系统最后一次启动
时间: $ sysctl kern.boottime
kern.boottime: { 秒 = 1271934886,
usec = 667779 } 四月 22 日星期四 12:14:46
2010年

哪些引用 sysctl(8),引用 sysctl(3)

The Uptime article on Wikipedia has an interesting lead:

Using sysctl

There is also a method of using sysctl
to call the system's last boot
time: $ sysctl kern.boottime
kern.boottime: { sec = 1271934886,
usec = 667779 } Thu Apr 22 12:14:46
2010

Which references sysctl(8), which references sysctl(3).

半山落雨半山空 2024-09-17 03:01:52

如果有人尝试使用 sysctl.h 以编程方式执行此操作,并且期望返回一个类似于命令行中看到的字符串,那么我得到的返回值是一个 16 字节数组,而不是字符串:

sysctlbyname("kern.boottime", value, &size, NULL, 0);

从 [0] 索引开始以十六进制形式放入 value 的内容的示例:

a9 af c6 4e 0 0 0 0 0 0 0 0 28 be 92 55

前 4 个字节(可能是前 8 个,直到 2012 年 1 月才知道)是小端字节顺序的纪元时间。

If anyone is trying to do this programmatically using sysctl.h and is expecting a string back like what you see in the command line, the returned value that I get is a 16 byte array, not a string:

sysctlbyname("kern.boottime", value, &size, NULL, 0);

An example for what gets put into value in hex starting from the [0] index:

a9 af c6 4e 0 0 0 0 0 0 0 0 28 be 92 55

The first 4 bytes (maybe the first 8, won't know until Jan 2012) is the epoch time in little endian byte order.

倥絔 2024-09-17 03:01:52

正确的方法:

CFTimeInterval getSystemUptime(void)
{
    enum { NANOSECONDS_IN_SEC = 1000 * 1000 * 1000 };
    static double multiply = 0;
    if (multiply == 0)
    {
        mach_timebase_info_data_t s_timebase_info;
        kern_return_t result = mach_timebase_info(&s_timebase_info);
        assert(result == noErr);
        // multiply to get value in the nano seconds
        multiply = (double)s_timebase_info.numer / (double)s_timebase_info.denom;
        // multiply to get value in the seconds
        multiply /= NANOSECONDS_IN_SEC;
    }
    return mach_absolute_time() * multiply;
}

您也可以使用 QuartzCore.framework 中的 CACurrentMediaTime() (可能具有相同的代码) - 自 OS X v10.5 和 iOS 2.0 起可用

correct way:

CFTimeInterval getSystemUptime(void)
{
    enum { NANOSECONDS_IN_SEC = 1000 * 1000 * 1000 };
    static double multiply = 0;
    if (multiply == 0)
    {
        mach_timebase_info_data_t s_timebase_info;
        kern_return_t result = mach_timebase_info(&s_timebase_info);
        assert(result == noErr);
        // multiply to get value in the nano seconds
        multiply = (double)s_timebase_info.numer / (double)s_timebase_info.denom;
        // multiply to get value in the seconds
        multiply /= NANOSECONDS_IN_SEC;
    }
    return mach_absolute_time() * multiply;
}

also you could use CACurrentMediaTime() from QuartzCore.framework (which has same code probably) - Available since OS X v10.5 and iOS 2.0

折戟 2024-09-17 03:01:52

为了补充 @Bleyddyn 的答案(因为已经有 11 年了,而且还在继续)...

我需要一个小实用程序来以秒为单位打印正常运行时间,所以我拿了该代码并稍微修改了一下,以便在现代 macOS 上进行编译。

您可以克隆 luckman212/uptime_s 存储库并运行 ./ build.sh 生成您自己的通用二进制文件,该二进制文件将简单地以秒为单位打印正常运行时间。

To add to @Bleyddyn's answer (since it's 11 years and counting)...

I needed a small utility to print the uptime in seconds, so I took that code and slightly altered it to compile on modern macOS.

You can clone the luckman212/uptime_s repo and run ./build.sh to generate your own universal binary that will simply print the uptime in secs.

电影里的梦 2024-09-17 03:01:52

DriverServices.h 中声明了一个函数 UpTime。我相信这相当于另一个函数mach_absolute_time。两者似乎都没有记录。

There is a function UpTime declared in DriverServices.h. I believe this is equivalent to another function mach_absolute_time. Both seem to be undocumented.

无需解释 2024-09-17 03:01:52

不幸的是,“sysctl kern.boottime”返回时间戳的秒数,而不是经过的秒数。多次调用不会增加秒数,但必须是从启动日期本身的纪元开始的秒数。

Unfortunately the "sysctl kern.boottime" returns the seconds of the timestamp, not elapsed seconds.. Multiple calls do not increase the second count, but must be seconds from epoc of the boot date itself.

于我来说 2024-09-17 03:01:52

一个简单的 Lua 脚本可以完全满足您的要求:

local now=tonumber(io.popen("date +%s"):read())
local boottime=tonumber(io.popen("sysctl -n kern.boottime"):read():match("sec = (%d+)"))
local uptime=now-boottime

A simple Lua script to do exactly what you ask for:

local now=tonumber(io.popen("date +%s"):read())
local boottime=tonumber(io.popen("sysctl -n kern.boottime"):read():match("sec = (%d+)"))
local uptime=now-boottime
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文