【问题标题】:How to get time duration of last system startup/reboot in OS X programatically?如何以编程方式获取 OS X 中上次系统启动/引导的持续时间?
【发布时间】:2013-12-30 00:39:46
【问题描述】:

如何以编程方式获取 OS X 中上次系统启动/重启的持续时间?

我只需要知道我的 OS X 重启速度有多快。

【问题讨论】:

    标签: objective-c c macos boot


    【解决方案1】:

    以下代码(来源于https://stackoverflow.com/a/14345786/1187415中的代码) 打印上次重启的时间:

    #include <stdio.h>
    #include <utmpx.h>
    
    int main(int argc, const char * argv[])
    {
        struct utmpx *bp;
        char *ct;
    
        setutxent_wtmp(0); // 0 = reverse chronological order
        while ((bp = getutxent_wtmp()) != NULL) {
            if (bp->ut_type == BOOT_TIME) {
                ct = ctime(&bp->ut_tv.tv_sec);
                printf("last reboot: %s", ct);
                break;
            }
        };
        endutxent_wtmp();
    
        return 0;
    }
    

    输出与命令行中“last reboot”的第一行相同。

    【讨论】:

    • 我需要知道上次启动加载过程花费了多少时间(例如 10 秒)。我不需要知道它是什么时候。
    • @Sergio:我似乎误解了你的问题,对此感到抱歉。 - 许多 OS X 服务是按需启动的,因此可能很难准确定义“启动持续时间”。 - 我想到的一个想法是您使用“Apple System Log”API(请参阅“man 3 asl”)查找最新的“rebo​​ot”消息,然后从那里找到下一个“loginwindow”消息。
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2016-08-24
    相关资源
    最近更新 更多