Jas
Jas

Reputation: 908

Linux uptime history

How can I get a history of uptime for my Debian box? After a reboot, I don't see an option for the uptime command to print a history of uptime. If it matters, I would like to use this uptime data for graphing a page in PHP to show my web servers uptime lengths between boots.

I'm 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. It sounds like a script is my best bet.

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

Upvotes: 41

Views: 56866

Answers (14)

sebisnow
sebisnow

Reputation: 2067

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

Upvotes: 1

1800 INFORMATION
1800 INFORMATION

Reputation: 135295

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

Upvotes: 54

Jan Schermer
Jan Schermer

Reputation: 11

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.

Upvotes: 1

none
none

Reputation:

Try this out:

last | grep reboot 

Upvotes: 20

paxdiablo
paxdiablo

Reputation: 881563

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.

Upvotes: 2

rfmoz
rfmoz

Reputation: 1111

You can use tuptime, a simple command for report the total uptime in linux keeping it betwwen reboots.

http://sourceforge.net/projects/tuptime/

Upvotes: 1

peterh
peterh

Reputation: 1

Nagios can make even very beautiful diagrams about this.

Upvotes: 0

sepehr
sepehr

Reputation: 5739

according to last manual page:

The pseudo user reboot logs in each time the system is rebooted. Thus last reboot will show a log of all reboots since the log file was created.

so last column of #last reboot command gives you uptime history:

#last reboot
reboot   system boot  **************** Sat Sep 21 03:31 - 08:27 (1+04:56)   
reboot   system boot  **************** Wed Aug  7 07:08 - 08:27 (46+01:19)

Upvotes: 16

Slet
Slet

Reputation: 11

Or you can use tuptime https://sourceforge.net/projects/tuptime/ for a total uptime time.

Upvotes: 1

roo
roo

Reputation: 7196

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.

Upvotes: 35

etchasketch
etchasketch

Reputation: 626

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.

Upvotes: 30

Shermozle
Shermozle

Reputation: 263

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.

Upvotes: 1

John Boker
John Boker

Reputation: 83709

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.

Upvotes: 1

Kyle Cronin
Kyle Cronin

Reputation: 79103

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/

Upvotes: 0

Related Questions