11

I'm aware of the uptime command, but it returns seconds since booted, so if I just substract that number from current timestamp, in theory I can get a different result if second changes after I've read the uptime and current timestamp. uptime -s is what I want, but it is not available on centos (how is it calculated btw?). Can I just get ctime of /proc dir? This seems to give me the proper number, but I wonder if every linux system has /proc created on boot.

  • What format do you want? HH:MM:SS? Something else? – terdon Oct 30 '14 at 14:38
  • 1
    @terdon, UNIX timestamp – Fluffy Oct 30 '14 at 14:48
11

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
  • 1
    A minor caveat is that the boot time is determined from the RTC, or some default if lacking an RTC. This time is written to utmp (by init) as the "system boot" entry, and read by who -b. The system time might be wrong until well into the startup. who -b on my rPi says it booted in 1970, and another RTC-less ARM says 2008 ;-) – mr.spuratic Mar 23 '17 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.