linux uptime history

Viewed 48921

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.

14 Answers

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

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.

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.

Try this out:

last | grep reboot 

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.

I would create a cron job to run at the required resolution (say 10 minutes) by entering the following [on one single line - I've just separated it for formatting purposes] 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 ten minutes while the machine is running. You can then examine this file manually to figure out the information or write a script to process it as you see fit.

Whenever the uptime reduces, there's been a reboot since the previous record. When there are large gaps between lines (i.e., more than the expected ten minutes), the machine's been down during that time.

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.

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/

Since I haven't found an answer here that would help retroactively, maybe this will help someone.

kern.log (depending on your distribution) should log a timestamp. It will be something like: 2019-01-28T06:25:25.459477+00:00 someserver kernel: [44114473.614361] somemessage

"44114473.614361" represents seconds since last boot, from that you can calculate the uptime without having to install anything.

Nagios can make even very beautiful diagrams about this.

Use Syslog

For anyone coming here searching for their past uptime. The solution of @1800_Information is a good advise for the future, but I needed to find information for my past uptimes on a specific date.

Therefore I used syslog to determine when that day the system was started (first log entry of that day) and when the system was shutdown again.

Boot time

To get the system start time grep for the month and day and show only the first lines:

sudo grep "May 28" /var/log/syslog* | head

Shutdown time

To get the system shutdown time grep for the month and day and show only the last few lines:

sudo grep "May 28" /var/log/syslog* | tail
Related