哪个文件包含最近的启动时间

哪个文件包含最近的启动时间

是否有一个文件记录计算机上次启动的时间?我不喜欢使用任何 shell 命令,例如who -b.

答案1

很可能是/var/run/utmp/var/run/utmpx

我使用 strace 来确定访问了哪些文件。

$ strace who -b  &| grep open
...
access("/var/run/utmpx", F_OK)          = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/var/run/utmp", O_RDONLY|O_CLOEXEC) = 3
...

然后您可以读取man 5 utmp包含登录记录的内容。

此外,从手册中您可以得到它有 BOOT_TIME 的信息

       The file is a sequence of utmp structures, declared as follows  in  <utmp.h>  (note
       that  this is only one of several definitions around; details depend on the version
       of libc):

           /* Values for ut_type field, below */

           #define EMPTY         0 /* Record does not contain valid info
                                      (formerly known as UT_UNKNOWN on Linux) */
           #define RUN_LVL       1 /* Change in system run-level (see
                                      init(1)) */
           #define BOOT_TIME     2 /* Time of system boot (in ut_tv) */

您可以在维基百科上阅读更多相关信息https://en.wikipedia.org/wiki/Utmp

相关内容