tags:

views:

1841

answers:

10

How can I get a history of uptimes for my debian box? After a reboot, I dont see an option for the uptime command to print a history of uptimes. If it matters, I would like to use these uptimes for graphing a page in php to show my webservers uptime lengths between boots.

Update: Not sure if it is based on a length of time or if last gets reset on reboot but I only get the most recent boot timestamp with the last command. last -x also does not return any further info. Sounds like a script is my best bet.

Update: Uptimed is the information I am looking for, not sure how to grep that info in code. Managing my own script for a db sounds like the best fit for an application.

A: 

i dont think this information is saved between reboots.

if shutting down properly you could run a command on shutdown that saves the uptime, that way you could read it back after booting back up.

John Boker
+1  A: 

the last command will give you the reboot times of the system. You could take the difference between each successive reboot and that should give the uptime of the machine.

update

1800 INFORMATION answer is a better solution.

roo
This however doesn't take into account shutdowns. The pseudo user reboot only appears in last when the system reboot.
freespace
Would that include account reboots as well?
roo
A: 

You could record the uptime in a db or file every 5 minutes and print it in your graph so if the server restarts you could see the uptime in graph as a peak

nicudotro
A: 

This information is not normally saved. However, you can sign up for an online service that will do this for you. You just install a client that will send your uptime to the server every 5 minutes and the site will present you with a graph of your uptimes:

http://uptimes-project.org/

Kyle Cronin
+2  A: 

This isn't stored between boots, but The Uptimes Project is a third-party option to track it, with software for a range of platforms.

Another tool available on Debian is uptimed which tracks uptimes between boots.

Shermozle
+4  A: 

You could create a simple script which runs uptime and dumps it to a file.

uptime >> uptime.log

Then set up a cron job for it.

etchasketch
+12  A: 

Install uptimed. It does exactly what you want.

Edit:

You can apparantly include it in a PHP page as easily as this:

<? system("/usr/local/bin/uprecords -a -B"); ?>

Examples

1800 INFORMATION
A: 

I would create a cron job to run at the required resolution (say 10 minutes) by entering the following [one single line] in your crontab (cron -l to list, cron -e to edit).

0,10,20,30,40,50 * * * * /bin/echo $(/bin/date +%Y-%m-%d) $(/usr/bin/uptime) >>/tmp/uptime.hist 2>&1

This appends the date, time and uptime to the uptime.hist file every 5 minutes while the machine is running. You can examine this file manually to figure out the information or write a script to process it as you see fit (hint, when the uptime reduces, there's been a reboot, when there are large gaps between lines (i.e., more than 10 minutes), the machine's been down). Cheers, Pax.

paxdiablo
A: 

last | grep reboot

Works for me :)

+1  A: 

Holey uptime batman, what kind of operation do you people run when you need to log your uptime on 5 minute intervals?

What's wrong with logging this on a daily (or twice, etc...) basis?