[I caught some misconceptions here which I think will this post clear off eventually]
There should not be any difference since both refer to /var/run/utmp
file, which has its own format to store the records. If at all there is any difference, then your utmp file is busted. uptime shows the amount of time that has passed since the system has been booted or how long the system has been running. It does not tell you the system clock or system boot time . System boot time information is stored /var/run/wtmp
file.
[centos@centos temp]$ date; uptime; who -b
Fri Dec 9 20:41:40 IST 2011
20:41:40 up 1:32, 2 users, load average: 0.50, 0.37, 0.29
system boot 2011-12-09 19:11
uptime refers as well /proc/uptime, which essentially keeps the counters in kernel.
[centos@centos temp]$ sleep 1; cat /proc/uptime; uptime; sleep 5; cat /proc/uptime ; uptime
5914.79 5271.83
20:47:39 up 1:38, 2 users, load average: 0.29, 0.31, 0.27
5920.07 5276.80
20:47:44 up 1:38, 2 users, load average: 0.56, 0.36, 0.29
/var/run/wtmp
is referred by last/lastb commands. who
& w
refers /var/run/utmp
file. last reboot
will show a log of all reboots since the log file was created.
Additionally, if you are having /proc filesystem, then tool such as procinfo
can give you bootup time as well.
Example:
bash$ procinfo | grep Bootup
Bootup: Wed Mar 21 15:15:50 2001 Load average: 0.04 0.21 0.34 3/47 6829
On any POSIX-compliant system, you can use the etime
column of ps
.
LC_ALL=POSIX ps -o etime= -p $PID
The output is broken down into days, hours, minutes and seconds with the syntax [[dd-]hh:]mm:ss
. You can work it back into a number of seconds with simple arithmetic:
t=$(LC_ALL=POSIX ps -o etime= -p $PID)
d=0 h=0
case $t in *-*) d=$((0 + ${t%%-*})); t=${t#*-};; esac
case $t in *:*:*) h=$((0 + ${t%%:*})); t=${t#*:};; esac
s=$((10#$d*86400 + 10#$h*3600 + 10#${t%%:*}*60 + 10#${t#*:}))
Best Answer
First of all,
crtime
is tricky on Linux. That said, running something likeor
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
:From
man who
: -b, --boot time of last system bootYou can convert that to seconds since the epoch using GNU
date
: