18

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.

Fluffy
  • 2,077
  • 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
  • I know this question is old but we should define what someone expects by "reliable." While I was developing xcron, I discovered that OSes don't really maintain accurate boot times. One would think that the boot timestamp value would be statically stored somewhere immutable. However, many systems seem to drift their boot time by about 1 second for each day of uptime. And some systems are even unreliable with being anywhere near reality for their boot time especially after they have been up for a few months. – CubicleSoft Feb 23 '22 at 01:53

4 Answers4

20

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
terdon
  • 242,166
  • 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
5

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

  • This solution has the advantage of not being vulnerable to RTC (real-time-clock) issues (such as noted by mr.spuratic's comment on the other answer). – Jay Sullivan May 17 '20 at 20:58
1

On macOS, /proc is unavailable, but /dev is. Here are some other options:

$ who -b
reboot   ~        Dec 17 18:45
$ stat -f %m /dev
1608227137

where -f %m specifies to show last modified time in "strftime(3) format".

$ stat -F /dev   
dr-xr-xr-x 3 root wheel 4606 Dec 17 18:45:37 2020 /dev/

If GNU tools was installed (via Homebrew), these are also available:

$ gwho -b
         system boot  2020-12-17 18:45
$ gstat -c %Y /dev
1608227137

where -c %Y specifies to show "time of last data modification, seconds since Epoch".

$ gstat -c %y /dev
2020-12-17 18:45:37.000000000 +0100

%y human readable instead.

  • 1
    Given that /dev is found more pervasively than /proc on UNIX and UNIX-like systems, I would submit that this is the best and most portable answer. Beware though, that who does not specify a year, nor indicate whether the time is UTC or local. – Jim L. Apr 23 '21 at 20:24
  • 1
    Unfortunately, using the last modified timestamp of /dev is unreliable. I've got a system that has an actual boot date of Oct 19 but a last modified date of Oct 21 for /dev (i.e. a two day discrepancy). The creation timestamp is either empty or the same as the last modified timestamp depending on the kernel in use. – CubicleSoft Nov 06 '21 at 20:45
  • @JimL. Historically, /dev was just an ordinary directory on the root filesystem, and so its timestamps would reflect the time the system was installed (or last configured), nothing to do with last boot. Over the last 20 years or so, many systems (including macOS and most Linux distributions) have turned /dev into an in-memory filesystem, so its creation time should be the most recent boot. (Last modified, however, may be more recent, if devices are added/removed after boot). So, this is not really that portable, on many old/obscure Unix(-like)s it will give a completely wrong answer – Simon Kissane Feb 28 '23 at 08:10
1

The above solutions only work on computers with an RTC. On a Raspberry Pi and other embedded systems without an RTC, the system will have the reference timestamp set to 0 when the system directory structure is created. Since it is Linux we're talking about here, read the info from /proc/uptime

cat /proc/uptime | awk '{ print $1 }'
Kavli
  • 111