1

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.

7 Answers 7

3

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

3
  • yup, I was thinking about this, maybe even get init.d script to run in shutdown.
    – aisbaa
    Aug 23, 2012 at 12:14
  • that's also an option.. so i guess i've answered what you actually needed..
    – aliasgar
    Aug 23, 2012 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, 2012 at 15:01
2

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)
2

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 {
    weekly
    dateext
    rotate 99999
    compress
}
EOF

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))'
0
1

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
  start)
        start
        ;;
  *)
        echo Usage: $0 start
        ;;
esac

$ 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
1

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

# tuptime -l
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.

Regards,

0

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.

http://podgorny.cz/moin/Uptimed

1
0

journalctl does it for you:

-> % journalctl --list-boots
-40 979a9d6333fa456ca3dd454904423f7d Tue 2021-02-09 18:56:00 UTC—Thu 2021-02-11 23:53:07 UTC
-39 4e1cf6ede2574080a2e767991f9a6f88 Fri 2021-02-12 09:20:30 UTC—Fri 2021-02-12 22:52:42 UTC
-38 c61db5934a314044b2aa89675f3174a4 Sat 2021-02-13 11:20:51 UTC—Sat 2021-02-13 17:43:47 UTC
-37 4c17abec4dab43eea06e1886105c6421 Sat 2021-02-13 18:57:54 UTC—Sat 2021-02-13 18:45:40 UTC
-36 01bb21d727494bdb8d2e0594cdc3bff4 Sat 2021-02-13 18:46:41 UTC—Sun 2021-02-14 00:32:35 UTC
-35 5620703289264409ac6fba815ee3a405 Sun 2021-02-14 12:34:13 UTC—Sun 2021-02-14 14:09:35 UTC
-34 f3bd6be41a1946e196100a0f6814696b Sun 2021-02-14 14:28:39 UTC—Mon 2021-02-15 00:15:46 UTC
-33 2560f26a043d4fa080e113570c024ef3 Mon 2021-02-15 09:13:37 UTC—Tue 2021-02-16 00:40:29 UTC
-32 5ffa0830150f4b0aad5060ebafe0579d Tue 2021-02-16 09:41:46 UTC—Tue 2021-02-16 23:53:54 UTC
-31 a277425897734813bda531603da792a6 Wed 2021-02-17 09:54:17 UTC—Wed 2021-02-17 23:51:29 UTC
-30 d3cffbc7373b45bb8c803d1aacfb4c42 Thu 2021-02-18 08:49:56 UTC—Thu 2021-02-18 23:27:59 UTC
-29 18d8740486ae47c9b8431a8ab9ec1b08 Fri 2021-02-19 09:39:38 UTC—Fri 2021-02-19 21:40:16 UTC
-28 3b96b65f6a4a42bca74f0e0eefcdacc4 Sat 2021-02-20 12:47:30 UTC—Sat 2021-02-20 22:14:05 UTC
-27 d1b067adffd64d89856461c030806800 Sun 2021-02-21 12:10:42 UTC—Mon 2021-02-22 22:10:42 UTC
-26 317279eedab14f5fb58cbd5fa4216be9 Tue 2021-02-23 08:56:50 UTC—Wed 2021-02-24 21:38:19 UTC
-25 fe1960acffef4730b5ae322779ac8f92 Thu 2021-02-25 09:00:05 UTC—Fri 2021-02-26 21:04:27 UTC
-24 fa6888263e0f4573b32efd0b3fe44485 Fri 2021-02-26 21:38:50 UTC—Tue 2021-03-02 12:42:35 UTC
-23 1e30020c8af340b3a8402c39aa961366 Wed 2021-03-03 09:25:07 UTC—Wed 2021-03-03 23:39:48 UTC
-22 4d0a03416e584f7982c8d753c3e33b4c Thu 2021-03-04 09:24:13 UTC—Fri 2021-03-05 22:47:20 UTC
-21 2baef30e20444afc89e3b1c69d9c2953 Sat 2021-03-06 10:02:04 UTC—Sat 2021-03-06 22:49:54 UTC
-20 b6e817adf27247d3a98d6585b7a44504 Sun 2021-03-07 10:05:16 UTC—Mon 2021-03-08 18:15:57 UTC
-19 0a61263956da49deb5c29576dd99442a Mon 2021-03-08 18:16:41 UTC—Mon 2021-03-08 23:30:38 UTC
-18 929a363340a54ab683732a8e29237731 Tue 2021-03-09 09:21:29 UTC—Wed 2021-03-10 16:46:59 UTC
-17 a4c7da289f7541f8a90e71494bcddecb Wed 2021-03-10 16:47:40 UTC—Wed 2021-03-10 23:02:51 UTC
-16 954962cf43b84ff784cc998e580b27bc Thu 2021-03-11 08:59:29 UTC—Sat 2021-03-13 00:50:38 UTC
-15 36ebb83d0db548429266cdda742232b0 Sat 2021-03-13 09:47:20 UTC—Sat 2021-03-13 22:46:45 UTC
-14 606f26d5ae7a4b9fa7c0c633ee3d9392 Sun 2021-03-14 10:36:25 UTC—Wed 2021-03-17 22:52:38 UTC
-13 41abbaa6dc724cb886c814b0930395af Thu 2021-03-18 09:10:48 UTC—Thu 2021-03-18 17:43:26 UTC
-12 261bdcf9ed0a41659250b9cf76d2b21b Thu 2021-03-18 17:44:12 UTC—Thu 2021-03-18 22:22:14 UTC
-11 d74e766768e1484bbf0de082aaf7d996 Fri 2021-03-19 08:51:49 UTC—Sat 2021-03-20 23:36:31 UTC
-10 a9a1a966e3864e919e806e52d1966398 Sun 2021-03-21 10:54:47 UTC—Mon 2021-03-22 22:36:35 UTC
 -9 0bcfba4461bd4c71a0a91941f0f4ed97 Mon 2021-03-22 22:38:36 UTC—Mon 2021-03-22 23:13:25 UTC
 -8 b8bd5280be624948a50e6fbd7bd9bdae Tue 2021-03-23 08:51:35 UTC—Tue 2021-03-23 22:54:03 UTC
 -7 eeefd7f87f974017b00f1fec18f6ab35 Wed 2021-03-24 09:23:47 UTC—Wed 2021-03-24 21:27:10 UTC
 -6 5a21602ec42d49129ebaeb00fafdc169 Thu 2021-03-25 09:02:57 UTC—Thu 2021-03-25 23:31:46 UTC
 -5 a35d7b829bf14c0a8131eb55737ec180 Fri 2021-03-26 00:11:28 UTC—Fri 2021-03-26 00:11:48 UTC
 -4 0200a2ffd26e474db477c18908b68299 Fri 2021-03-26 08:43:22 UTC—Fri 2021-03-26 23:14:12 UTC
 -3 5bedcc4337634556aa92f087595328aa Sat 2021-03-27 09:56:44 UTC—Mon 2021-03-29 07:50:43 UTC
 -2 e3f09e82362b4644b89be1cb29fa7c7c Mon 2021-03-29 07:51:24 UTC—Tue 2021-03-30 23:09:29 UTC
 -1 d868669ccca04ed18d3de0789a18e052 Wed 2021-03-31 07:54:37 UTC—Wed 2021-03-31 20:53:09 UTC
  0 e016756d5af94aeeb82a369451d22b26 Thu 2021-04-01 07:45:35 UTC—Thu 2021-04-01 10:10:25 UTC

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.