LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise
User Name
Password
Linux - Enterprise This forum is for all items relating to using Linux in the Enterprise.

Notices


Reply
  Search this Thread
Old 09-28-2012, 12:32 AM   #1
manoj.linux
Member
 
Registered: Aug 2007
Posts: 284

Rep: Reputation: 17
Question Finding Uptime of Server.


Hi,

I would like to is it possible to find out uptime of server after server rebooting?
 
Old 09-28-2012, 12:48 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the command last will give you info about the reboots.
From the other hand there is a command uptime.
 
1 members found this post helpful.
Old 09-28-2012, 12:50 AM   #3
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 09-28-2012, 01:05 AM   #4
manoj.linux
Member
 
Registered: Aug 2007
Posts: 284

Original Poster
Rep: Reputation: 17
Thanks Zhjim for your quick response.
 
Old 09-28-2012, 01:12 AM   #5
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 09-28-2012, 01:31 AM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Little script to compute overall uptime

Code:
#!/bin/bash
while read line; do
# *edit start*
        # clear last run data
        day=0
        hour=0
        minute=0

        # 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)
        fi
# *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))
        fi
# *edit stop*
	if [ $hour -gt 0 ]; then
		HOUR=$(($HOUR + $hour))
	fi
	if [ $minute -gt 0 ]; then
		MIN=$(($MIN + $minute))
	fi
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"

Last edited by zhjim; 09-28-2012 at 08:08 AM.
 
Old 09-28-2012, 07:18 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
"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).

Code:
ps -p $(pidof -x init) -o lstart
                 STARTED
Tue Sep 25 08:52:20 2012
Useful? Maybe.

Credit goes to fernfrancis...
 
Old 09-28-2012, 07:41 AM   #8
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.

Last edited by zhjim; 09-28-2012 at 08:04 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
init uptime longer than system uptime? m4rtin Linux - Server 1 09-15-2010 07:45 AM
Finding out interface uptime GSMD Linux - Networking 4 12-13-2007 04:06 PM
uptime for server einstien Linux - General 2 09-20-2004 08:35 PM
Server uptime discrepancy peppiv Linux - Software 2 05-17-2004 11:41 AM
Remote Server Uptime duerra Linux - General 6 05-12-2004 05:50 PM

LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise

All times are GMT -5. The time now is 12:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration