3

I would like to know how long my MacBook Pro was on today - total uptime for the day. I just bought it, and I thought the battery was bad but it went on for longer than I thought. Of course, I wasn't standing next to it with a stopwatch... It would be a great feature for me to track time spent on devices anyways, to see which ones I spend the most time on. Maybe in System Report, or using a tool in Utilities? A script?

EDIT: I am asking this to do this on different computers, some built before 2011.

5

I have uploaded a bash script as a Gist to my Github account at print_osx_uptimes.sh which may help answer your question, even if not exactly what was asked (which was more complicated).

The script will query the Apple System Log (ASL) facility for 'shutdown' messages. The 'Message' field’s values contain the system uptime as of the shutdown event, which I figured is probably reasonably close to the original poster’s question.

It then converts the uptime seconds to a more readable format showing days, hours, minutes and seconds.

Tested on OS X 10.10.5, but see no reason it should not work on anything from 10.7 onward (likely 10.6 as well).

Edited code to also check for (duh) 'reboot' messages, and updates the output to show what the event type (ie. Sender name) was for each.

Sample output:

$ ./print_osx_uptimes.sh
Shutdown on date/time:         Event                     Reported uptime of:
=============================  =========  ===================================
Mon Jul  6 2015 10:32:21 EDT   shutdown       1 days  11 hrs  30 min  15 secs
Thu Jul 16 2015 20:47:24 EDT   reboot         1 days  22 hrs  15 min  46 secs
Thu Jul 16 2015 22:01:50 EDT   shutdown       0 days  21 hrs   4 min  28 secs
Sun Jul 19 2015 02:10:34 EDT   shutdown       2 days  16 hrs  35 min  35 secs
Tue Jul 21 2015 19:32:35 EDT   shutdown       9 days  17 hrs  41 min  46 secs
Fri Jul 24 2015 20:35:05 EDT   shutdown       9 days   7 hrs  55 min  38 secs
Sat Jul 25 2015 00:03:13 EDT   shutdown       8 days  10 hrs  45 min   8 secs
Tue Jul 28 2015 09:16:47 EDT   shutdown       5 days  21 hrs  20 min  57 secs
Thu Jul 30 2015 22:32:36 EDT   shutdown      10 days   0 hrs   3 min  59 secs
Tue Aug  4 2015 01:37:32 EDT   shutdown       7 days   4 hrs  24 min  35 secs
Fri Aug 14 2015 19:15:04 EDT   reboot         5 days  22 hrs  42 min   1 secs

The meat of the script on Github Gist is:

#!/bin/bash

# https://gist.github.com/jps3/afc1e374b632e1ed1ebc

# function displaytime() is a modified form of that from:
# http://unix.stackexchange.com/a/27014
function displaytime {
  local T=$1
  local D=$((T/60/60/24))
  local H=$((T/60/60%24))
  local M=$((T/60%60))
  local S=$((T%60))
  printf '%5s days  %2s hrs  %2s min  %2s secs' $D $H $M $S
}

function get_syslog_entries () {
  syslog -d store \
    -F '$(Sender) $(Message)' \
    -k Sender Req '^(shutdown|reboot)$' \
    -k Message Req "SHUTDOWN_TIME"
}

while read -a line; do
  sender="${line[0]}"
  log_entry_timestamp="$(date -j -f %s "${line[2]}" +"%a %b %e %Y %T %Z")"
  uptime_timestamp="$(displaytime "${line[3]}")"
  printf "%s   %-8s   %s\n" "${log_entry_timestamp}" "${sender}" "${uptime_timestamp}"
done < <(get_syslog_entries)

Hope this helps. I tried to clean up the script and make the parts and comments useful.

  • + from me! Logging the shutdown requests then parsing by date should get the OP's desired data. – bjbk Aug 30 '15 at 4:44
  • This looks great guys! How do I run these scripts? I'm kind of a dork when there's no GUI :P – MicroMachine Nov 11 '15 at 1:54
1

You may be able to calculate based on total uptime.

See this article on osXdaily.

How to Check a Mac's Uptime

To check your Mac's uptime, simply type ‘uptime’ in the Terminal. Terminal is an application found within /Applications/Utilities/ and is included in every version of Mac OS X.

uptime

Hit the return key and you’ll find the answer.

To check daily uptime with multiple shut-downs in the day, run this command before shutting down and log the result somewhere such as a spreadsheet or text file. Then it is an easy calculation based on date and duration. But really, unless you have poor security, there is not much need to shutdown very often. Sleep uses very little power.

See this Apple Support article:

Standby Mode

For Mac computers that are started from an solid-state drive, OS X includes a deep sleep mode known as Standby Mode. Mac computers manufactured in 2013 or later enter standby after one to three hours of regular sleep. A computer with a fully charged battery can remain in standby for up to thirty days without being plugged in to an AC power source.

  • Thanks! How should I interpret the answer, and find what period of time it relates to? Mine says: Last login: Fri Aug 21 18:47:43 on console Fabrices-MBP:~ fabrice$ uptime 22:16 up 3:30, 2 users, load averages: 1.67 2.20 2.92 Fabrices-MBP:~ fabrice$ – MicroMachine Aug 22 '15 at 5:17
  • @fabriced It means: time now, uptime 3 hours 30 minutes, 2 users logged in (fabrice at s000 & fabrice at console) and the Load Triplet: load in the last minute, load in the last 5 minutes and finally load in the last 15 minutes (with load: dimensionless number) – klanomath Aug 22 '15 at 6:40
  • Ok! But uptime seems to me like it's how long the computer was "on". I turned it on this norming, then off, then came home and turned it back on. Is this uptime for all day today or only since I came home? – MicroMachine Aug 22 '15 at 8:44
  • Uptime is reset whenever the computer is rebooted. If you put the computer to sleep instead of turning it off, it is possible to have an excessively long uptime (over a year or more) because sleep/wake does not affect the uptime. – Kent Aug 29 '15 at 22:16
  • Ok, thanks! My original question was how to know precise total uptime even if there's been reboots. Thanks tho – MicroMachine Nov 16 '15 at 10:13
1

A third party tool such as DssW Sleep Monitor can be used to track the amount of time your Mac was powered on. The application tracks power and battery use. Totals are available in the applications' Info panel.

DssW Sleep Monitor on OS X

I wrote Sleep Monitor, so am bias. Also please note Sleep Monitor 3 works on OS X 10.7 – 10.9.

  • Ok! I'll try it and let you know how it goes! – MicroMachine Nov 9 '15 at 4:03
  • So if I understand correctly I have to launch the app at startup of the computer and never quit it? – MicroMachine Nov 11 '15 at 1:53
  • Sleep Monitor asks when started if you want to launch it each time, or allow it to run in the background. In the latter state, you do not need to launch Sleep Monitor each time – additionally, logging will then continue even when no-one is logged in. – Graham Miln Nov 11 '15 at 9:15
  • Doesn't work for me at the moment; starting up takes a lot of time and doesn't remember past days – MicroMachine Nov 11 '15 at 20:14
  • What version of OS X are you using? If you want help sorting it out, please get in touch via support@dssw.co.uk – Graham Miln Nov 12 '15 at 5:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .