🐧 How to determine when the Linux operating system was booted

There is a simple solution, because we can directly use the / proc filesystem.

Use the / proc / stat file which contains kernel / system statistics.

$ 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

Let’s calculate the btime value, which contains the loading time in seconds since the start.

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

Convert seconds from start to date using awk, xargs and date.

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

Convert seconds elapsed from start to date using awk and date.

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

Convert seconds to date using only 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