Uptime since boot excluding sleep for osx yosemite?

Solution 1:

As far as I know there is no simple command executed in Terminal to get your desired result.

Here is an example for a workaround to get the uptime and the sleep time(s):

uptime && pmset -g log|grep 'date'|grep -E ' Wake | Sleep '|grep -v 'Dark Wake'

with date: dd/mm/yy (example: 23/07/15)

Example (10.9.5 Virtual Machine):

machinename:~ user$ uptime && pmset -g log|grep '23/07/15'|grep -E ' Wake | Sleep '|grep -v 'Dark Wake'
14:59  up  1:09, 3 users, load averages: 0.58 0.47 0.51
23/07/15 13:51:39 GMT+2  Sleep                  Software Sleep pid=90: Using AC                                             10 secs   
23/07/15 13:51:49 GMT+2  Wake                   Wake [CDNVA] due to power-button/User: Using AC                             149 secs  
23/07/15 13:54:18 GMT+2  Sleep                  Software Sleep pid=90: Using AC                                             91 secs   
23/07/15 13:55:49 GMT+2  Wake                   Wake [CDNVA] due to power-button/User: Using AC                             1872 secs 
23/07/15 14:27:01 GMT+2  Sleep                  Software Sleep pid=90: Using AC                                             7 secs    
23/07/15 14:27:08 GMT+2  Wake                   Wake [CDNVA] due to power-button/User: Using AC

Now do the math and calculate the non-sleep uptime. (6540 secs - 108 secs = 6432 secs).

Depending on your operating system and the hibernation mode you set, you may have to alter the grep part in the above command. Check pmset -g log for the appropriate strings. You also may omit the ' Wake ' part of the command to simplfy the output.

Solution 2:

execute the following script with python3

# imports
import os
from datetime import datetime

# global declarations
sleepAndWakes = []
cummulatedAwakeTime =  datetime.strptime('0001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
previousSleepAndWakeWithTime = None

resetAwakeStatisticsByLastBoot = None

class SleepAndWakeWithTime:
    def __init__(self, dateTime, actionName):
        self.dateTime = dateTime
        self.actionName = actionName


# reading log messages vi pmset
stream = os.popen('pmset -g log|grep -e " Sleep  " -e " Wake  " -e " DarkWake  " -e " ShutdownCause " -e " Start "')
lines = stream.readlines()

# parse pmset lines and store them in an array of event and date-time
for line in lines:
    #print('line: ' + line)
    dateTimeStr =  line[:19]
    actionName = line[26:34].strip()
    datetimeObject = datetime.strptime(dateTimeStr, '%Y-%m-%d %H:%M:%S')
    sleepAndWakes.append(SleepAndWakeWithTime(datetimeObject, actionName))

# read array and calcualate the total wake time (all the times between sleeps)
for index, sleepAndWakeWithTime in enumerate(sleepAndWakes):
    #print(sleepAndWakeWithTime.dateTime.strftime('%Y-%m-%d %H:%M:%S') + ' ' + sleepAndWakeWithTime.actionName)
    if index > 0: 
        previousSleepAndWakeWithTime = sleepAndWakes[index-1].dateTime
        timeDifference = sleepAndWakeWithTime.dateTime - previousSleepAndWakeWithTime
        if sleepAndWakeWithTime.actionName == 'Sleep' or sleepAndWakeWithTime.actionName == 'Shutdown': 
            cummulatedAwakeTime += timeDifference; #print(timeDifference)

        # ask to reset statistics after boot (and then reset if 'y' was typed)
        if sleepAndWakeWithTime.actionName == 'Start':
            if resetAwakeStatisticsByLastBoot == None:
                resetAwakeStatisticsByLastBootAnswer = input("Do you want to calculate/cummulate the total awaketime since the last boot [y] or the total awake time [n]?")
                if resetAwakeStatisticsByLastBootAnswer == 'y': resetAwakeStatisticsByLastBoot = 'y'
                elif resetAwakeStatisticsByLastBootAnswer == 'n': resetAwakeStatisticsByLastBoot = 'n'
                else: 
                    print('insufficient answer assuming yes')
                    resetAwakeStatisticsByLastBoot = 'y'
            if resetAwakeStatisticsByLastBoot == 'y': cummulatedAwakeTime =  datetime.strptime('0001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')

# add time since last sleep
cummulatedAwakeTime += (datetime.now() - previousSleepAndWakeWithTime)

# reduece the inital minimum date value
cummulatedAwakeTime -= datetime.strptime('0001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')

print('Total awake time : ' + str(cummulatedAwakeTime))