Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know that uptime prints the time a machine has been up and running, but is there an easier (reliable) way to get the date of the start up than counting down from this output?

I tried looking around /proc, but didn't find anything of relevance. There's also a line like this on my dmesg: [ 0.673492] rtc_cmos rtc_cmos: setting system clock to 2011-03-14 14:26:52 UTC (1300112812), but I'm wondering if this method is distribution and kernel version agnostic.

share|improve this question
    
What's unreliable or hard about uptime? –  Bobby Mar 28 '11 at 11:25
    
@Bobby: Nothing about the command or its function per se, but as said I want to get the date and time of last system boot, not how long it's been up since. uptime returns a string like "up 13 days, 21:01", and you'd need to count it from that. –  jho Mar 28 '11 at 11:29
3  
It's trivial to count back from the uptime value. If you want reliable, you want /proc/uptime. –  Sam Hocevar Mar 28 '11 at 14:29
add comment

6 Answers

up vote 13 down vote accepted

I found some commands here. Try who -b or last reboot | head -1.
who gives numeric dates, while last reboot returns abbreviated day / month names.

share|improve this answer
    
how about if we only want the date and nothing else? –  T0xicCode May 8 '12 at 3:00
2  
who -b | cut -d' ' -f13 returns date only (-f14 returns time) –  charlesbridge May 8 '12 at 11:15
    
Warning: last reboot did not give me the correct date ! who -b did. –  qwertzguy Sep 18 '13 at 18:08
add comment
  • good: who -b or parsing /proc/uptime
  • bad: ls -ld /proc/1 and variants.

Don't use ls -ld /proc/1 for this purpose. It's sometimes updated after s2disk or s2ram.

In my case, who -b said:

system boot May 2 09:51

While ls -ld /proc/1:

dr-xr-xr-x 7 root root 0 May 3 13:09 /proc/1

ls -ld for /proc or /sys seem to persist after resuming, but it's implementation dependent, and may change in future, so don't use such methods. And if your system clock is in localtime, not UTC, they have negative offset.

(I don't have the privilege to comment to answers yet, so opened a new answer. Sorry.)

share|improve this answer
add comment

I found the btime line in /proc/stat when poking around a bit

cat /proc/stat | grep btime | awk '{ print $2 }'

and after a quick search, i found this page: /proc/stat explained, which outlines the "Various pieces of information about kernel activity are available in the /proc/stat file."

The "btime" line gives the time at which the system booted, in seconds since the Unix epoch.

share|improve this answer
add comment

In Linux,

ls -ld /proc

seems to give me what I need. The post above is odd. /proc/uptime does not contain a date value – it would have to be subtracted from the current time. Maybe he meant:

date -d @$(( $(date +%s) - $(cut -f1 -d. /proc/uptime) ))
share|improve this answer
add comment

This queries the uptime from the kernel and displays it in the local timezone:

date -d "`cut -f1 -d. /proc/uptime` seconds ago"

Be careful about other options. The last command will stop working as soon as wtmp has been rotated. The who command depends on the availability and integrity of utmp. And /proc/1 might have the current date instead of the boot time date, and could even be unavailable on a hardened system. Edit: dmesg only has a fixed-length back buffer, so it is unrealiable, too. The kernel logs may be in /var/log but most distributions only keep 8 weeks of them.

share|improve this answer
add comment

The simplest way is to look to see when /sbin/init started (that's always the first process to be started after the kernel loads):

# ls -ld /proc/1
dr-xr-xr-x 7 root root 0 2011-03-27 23:54 /proc/1

So I can see that my machine booted up at 6 minutes to midnight on the 27th of March 2011.

If you want to use it in scripting you can use the stat command instead:

# stat --printf='%Y' /proc/1
1301266491

The %Y specifies the time since the directory was last changed (process creation time) in seconds since the epoch (1/1/70) and is a standard unix timestamp.

share|improve this answer
    
Unfortunately this doesn't work: the mtime on those folders can change for other reasons (I have a system here that has a 5 day uptime and the mtime of /proc/1 is 25 minutes ago) –  kdt Dec 18 '12 at 13:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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