-   Linux - Enterprise (
-   -   Finding Uptime of Server. (

manoj.linux 09-28-2012 01:32 AM

Finding Uptime of Server.

I would like to is it possible to find out uptime of server after server rebooting?

pan64 09-28-2012 01:48 AM

the command last will give you info about the reboots.
From the other hand there is a command uptime.

zhjim 09-28-2012 01:50 AM

The command uptime will show you how long your system is up.
But I guess what you want to know is an overall uptime. Like 10mins up -> reboot -> 2days up -> reboot -> 3weeks up. To a total of 3weeks 2days and 10minutes. Right? Sorry but that is not possible with build in tools. You have to "manually" do the math and save this between reboots or similar. There are some threads here on LQ how to do this.

manoj.linux 09-28-2012 02:05 AM

Thanks Zhjim for your quick response.

zhjim 09-28-2012 02:12 AM

Pan64 thanks for bringing up the last command. What I just found out is that the output lines of last with reboot on them also show you the time from when to when the system was up.

I learned something during my first cup of coffee. Must be a good day :)

zhjim 09-28-2012 02:31 AM

Little script to compute overall uptime


while read line; do
# *edit start*
        # clear last run data

        # if there are days we have more than 5 characters
        # hour:minute
        if [ ${#line} -gt 5 ]; then
                day=$(echo $line | cut -d '+' -f 1)
                line=$(echo $line | cut -d '+' -f 2)
# *edit stop*
        #remove leading zero to not go octal
        hour=$(echo $line| cut -d ":" -f 1 | sed 's/^0//')
        minute=$(echo $line | cut -d ":" -f 2| sed 's/^0//')
        #sum things up
# *edit start*
        if [ $day -gt 0 ]; then
                DAY=$(($DAY+ $day))
# *edit stop*
        if [ $hour -gt 0 ]; then
                HOUR=$(($HOUR + $hour))
        if [ $minute -gt 0 ]; then
                MIN=$(($MIN + $minute))
done < <(last | grep reboot | awk '{print $11}'| tr -d "(" | tr -d ")")
# 60 minutes per hour?
UP=$(($MIN / 60))
MIN=$(($MIN % 60 ))
HOUR=$(($HOUR + $UP))

# *edit start*
# 24 hours per day?
UP=$(($HOUR / 24))
HOUR=$(($HOUR % 24))
DAY=$(($DAY + $UP))
# *edit stop*

#echo "Overall uptime of $HOUR hours and $MIN minutes"
echo "Overall uptime of $DAY days, $HOUR hours and $MIN minutes"

Habitual 09-28-2012 08:18 AM

"last reboot" and "uptime" are good, but here's an alternative that I occassionally use now to find the 'uptime' of an individual process. (This process just happens to be init).


ps -p $(pidof -x init) -o lstart
Tue Sep 25 08:52:20 2012

Useful? Maybe.

Credit goes to fernfrancis...

zhjim 09-28-2012 08:41 AM

Whuhu. last reboot is even better. Running on a server I also saw that I don't have days in my script. Editing in progress.

All times are GMT -5. The time now is 05:02 AM.