How to reliably get timestamp at which the system booted?
First of all, crtime
is tricky on Linux. That said, running something like
$ stat -c %z /proc/
2014-10-30 14:00:03.012000000 +0100
or
$ stat -c %Z /proc/
1414674003
is probably exactly what you need. The /proc
file system is defined by the LFS standard and should be there for any Linux system as well as for most (all?) UNIXen.
Alternatively, assuming you don't really need seconds precision, but only need the timestamp to be correct, you can use who
:
$ who -b
system boot 2014-10-30 14:00
From man who
:
-b, --boot
time of last system boot
You can convert that to seconds since the epoch using GNU date
:
$ date -d "$(who -b | awk '{print $4,$3}' | tr - / )" +%s
1414674000
Another solution is /proc/stat
's btime
[1]:
$ cat /proc/stat | grep btime | awk '{print $2}'
Example output:
1583547431
This is a seconds-since-epoch timestamp.
[1] http://man7.org/linux/man-pages/man5/proc.5.html