🐧如何确定何时启动Linux操作系统

有一个简单的解决方案,因为我们可以直接使用/ proc文件系统。

使用包含内核/系统统计信息的/ proc / stat文件。

$ cat /proc/stat
cpu  13014200 24406 4799169 40794104 34102 0 96456 0 0 0
cpu0 3273277 5052 1208241 30257443 16180 0 30562 0 0 0
cpu1 3258376 6672 1187026 3510180 8958 0 25642 0 0 0
cpu2 3266882 6158 1193469 3501113 4998 0 29868 0 0 0
cpu3 3215665 6523 1210432 3525367 3964 0 10383 0 0 0
intr 539439101 9 389 0 0 0 0 0 15646 1 2595538 0 0 15 0 0 0 0 0 0 0 0 347 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 253 0 0 0 0 1483731 313 4800809 44745763 0 7901501 18 629 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 2042251595
btime 1578304868
processes 196905
procs_running 1
procs_blocked 0
softirq 310354343 39361834 94774221 414 442124 4311767 39 489577 97929159 0 73045208

让我们计算btime值,其中包含自启动以来的下载时间(以秒为单位)。

$ awk '/btime/ {print $2}' /proc/stat
1578304868

使用awk,xargs和日期将秒从开始转换为日期。

$ awk '/btime/ {print $2}' /proc/stat | xargs -IUNIX_TIMESTAMP date --date @UNIX_TIMESTAMP
Mon Jan  6 11:01:08 CET 2020

使用awk和date转换从开始到日期经过的秒数。

$ awk '/btime/ {command="date --date @" $2;system(command)}' /proc/stat
Mon Jan  6 11:01:08 CET 2020

仅使用awk将秒转换为日期。

$ awk '/btime/ {print strftime("%a %b %e %H:%M:%S %Z %Y", $2)}' /proc/stat
Mon Jan  6 11:01:08 CET 2020

Sidebar