3

I need to take the total working hours of the system in a day. That means, take time between power on and shutdown and also subtract the time of system were suspend. Final output must be total working hours in a day. I tried some third party softwares and my own command based operations but all that failed. How can i calculate that. Is there any way? I tried an idea to get the result like this:

  • system power on time : "time"
  • system suspend at : "time"
  • system wake up at : "time"
  • system shut downed at : "time"
  • total working hour: ((system shut downed - system power on) - (difference between sleep time and wake up time))

I think it's huge task. Can u please help me?

muru
  • 197,895
  • 55
  • 485
  • 740

2 Answers2

0

All the information needed is in journalctl. Not a complete answer but this script is a good start that you can tweak:

$ suspendtime
Oct 31 05:55:19 to Oct 31 16:54:26 lasting 39,547 seconds
Oct 31 23:21:21 to Nov 01 04:29:12 lasting 18,471 seconds
Nov 01 05:51:27 to Nov 01 17:08:34 lasting 40,627 seconds
Nov 02 00:01:33 to Nov 02 10:28:46 lasting 37,633 seconds
Nov 02 18:15:59 to Nov 02 19:10:14 lasting 3,255 seconds
Nov 02 21:17:33 to Nov 03 05:31:54 lasting 33,261 seconds
Nov 03 12:06:39 to Nov 03 14:22:50 lasting 8,171 seconds
Nov 03 22:28:12 to Nov 04 04:17:13 lasting 20,941 seconds
Nov 04 05:49:40 to Nov 04 16:48:52 lasting 39,552 seconds
Nov 04 21:45:48 to Nov 05 04:19:26 lasting 23,618 seconds
Nov 05 05:52:05 to Nov 05 16:32:38 lasting 38,433 seconds
Nov 05 21:12:18 to Nov 06 04:16:50 lasting 25,472 seconds
Nov 06 05:50:45 to Nov 06 16:22:54 lasting 37,929 seconds

Linux uptime 574,835 seconds (6 Days 15 Hours 40 Minutes 35 Seconds)
13 Suspends 366,910 seconds (4 Days 5 Hours 55 Minutes 10 Seconds)
Real uptime 207,925 seconds (2 Days 9 Hours 45 Minutes 25 Seconds)

The bash script already does the heavy lifting of calculating the amount of time suspended each day from journalctl. Modifying the script to get system boot and shutdown times would be a minor enhancement.

0

Note: This is only a partial answer, dealing only with the difference between uptime and suspended time.

You could determine the difference between uptime and suspended time utilizing some of the information available via /proc/timer_list.

Example 1:

$ uptime
 11:58:46 up  3:43,  2 users,  load average: 0.00, 0.01, 0.05
$ cat /proc/timer_list | grep "now at"
now at 12734872068331 nsecs

So uptime = (3 * 60 + 43) * 60 = 13,3800 seconds. Active time = 12,735 seconds. Therefore Suspended time = 13,300 - 12,735 = 645 seconds

Or, just look at the offset time for each CPU to get its suspended time directly:

$ cat /proc/timer_list | grep -A 1 "ktime_get_boottime"
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs
--
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs
--
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs
--
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs
--
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs
--
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs
--
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs
--
  .get_time:   ktime_get_boottime
  .offset:     668016000993 nsecs

Or 668 seconds for each CPU.
Why do the two methods differ by 23 seconds? Because uptime is only listed to the minute. So that answer is really 645 +59 -0 seconds (or, depending if uptime rounds, 645 +- 30 seconds.

So for what you seem to want run this during shutdown:

$ cat /proc/timer_list | grep "now at"
now at 25561396304263 nsecs

and divide that number by 3.6e12 (i.e. 7.1 hours active time)

Doug Smythies
  • 15,448
  • 5
  • 44
  • 61