I have Linux desktop. I don't leave it 24/7 turned on. I would like to gather statistics on how much time computer was turned on each day, f.e.:

2012-08-21: 7:52
2012-08-22: 8:43
2012-08-23: 7:36

If computer wasn't turned on, no record is required.


You can create a script which runs uptime & dumps it into a file.

uptime >> uptime.log

After that setup a cron job for it. To know more about how to create a cron job : Create Cron Job

Or you can sign-up for an online service to do it for you : Uptime Project

  • yup, I was thinking about this, maybe even get init.d script to run in shutdown. – aisbaa Aug 23 '12 at 12:14
  • that's also an option.. so i guess i've answered what you actually needed.. – aliasgar Aug 23 '12 at 12:30
  • this is really good answer, but I'm looking for more sophisticated solution. Maybe something like workrave, which has this as additional functionality, except I don't need this typing break solution. – aisbaa Aug 23 '12 at 15:01

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)

This logs the output of 'uptime' every five minutes. This output also includes the number of users currently logged on and load averages of your computer the last 1, 5 and 15 minutes:

sudo bash #root
mkdir /var/log/uptime
crontab -e

Add this line:

*/5  * * * * echo `date +\%Y\%m\%d;uptime` >> /var/log/uptime/uptime.log 2>&1

And to split+gzip the log into weekly files:

cat <<'EOF'. > /etc/logrotate.d/uptime
/var/log/uptime/uptime.log {
    rotate 99999

Then, after some time has passed, view your uptime for each day like this:

uplog() { (cd /var/log/uptime/;zcat uptime.log*gz;cat uptime.log) }
uplog|cut -c1-8|uniq -c|perl -aple'$_.=" ".("=" x ($F[0]/5))'

The number 288 (12*24) means the computer was up all day (although reboots shorter than five minutes may go undetected by this method).

Or similarly for each month like this:

uplog|cut -c1-6|uniq -c|perl -aple'$_.=" ".("=" x ($F[0]/150))'

There will be datestamped entries in syslog (/var/log/messages*) you can extract this information from.

You could also add K entries to /etc/rc0.d to, for example, to run logger to add a specific syslog record on shutdown. See http://www.debian-administration.org/articles/212 or equivalent for the Linux distribution you are using

$ vim /etc/init.d/uptime
$ cat /etc/init.d/uptime

# chkconfig: 0 99 1
# description: Record uptime at shutdown.

start() {
        uptime >> /var/log/uptime 2>&1

case "$1" in
        echo Usage: $0 start

$ chmod +x /etc/init.d/uptime

$ chkconfig --add uptime

$ chkconfig --list uptime
uptime          0:on    1:off   2:off   3:off   4:off   5:off   6:off

$ ls /etc/rc*d/*uptime*
/etc/rc0.d/S99uptime  /etc/rc3.d/K01uptime  /etc/rc6.d/K01uptime
/etc/rc1.d/K01uptime  /etc/rc4.d/K01uptime
/etc/rc2.d/K01uptime  /etc/rc5.d/K01uptime

$ /etc/rc0.d/S99uptime start

$ cat /var/log/uptime
 13:15:28 up 135 days, 12:06,  1 user,  load average: 0.07, 0.04, 0.01

I've found similar question on stackoverflow https://stackoverflow.com/questions/79490/linux-uptime-history . Best uptimed seems to be what I want, thank you guys for help.



Tuptime can do it when executing "tuptime -e", for example:

# tuptime -e
Startup:  1  at  08:55:01 AM 08/04/2015
Uptime:   24 seconds
Shutdown: OK  at  08:55:25 AM 08/04/2015

Downtime: 5 seconds

Startup:  2  at  08:55:31 AM 08/04/2015
Uptime:   20 seconds
Shutdown: OK  at  08:55:51 AM 08/04/2015

Depending on your locales, the date format can change.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.