DEV Community

Cover image for Find out your Mac's average daily usage!
Mark Lambe
Mark Lambe

Posted on

Find out your Mac's average daily usage!

So I got curious about how much value I've gotten from my Mac and decided to write a script that would tell me how many hours per day I've averaged since I got it, as well as how many days I've had it.

Alt Text

The final script isn't perfect but it's on npx so you can run it directly from there by just doing npx mac-usage-stats.

You can view the code on Github.

Unfortunately there is one tiny dependency, but there's a chance you have it installed already. It's a command line tool for Mac that helps you get hard-drive info called smartctl, which you can install very easily.

Alternatively if you prefer, I originally wrote it in Python and you can use that too!

I find it really interesting to know how much I've used my Mac over four and a half years, and now you can too! ☺️

Top comments (4)

Collapse
 
moopet profile image
Ben Sinclair • Edited

You say it only works on Macs, but smartmontools is available across many OSs. I can do the same thing on my Arch machine with smartctl -a /dev/sda for example. This falls down a little because you're unlikely to ever change the drive in a Mac (because you pretty much can't) whereas in a regular PC you can move it from one machine to the next.

Your timeSinceBought is also based on the date the setup finished. Won't that get reset every time you have to reinstall MacOS?

Collapse
 
marklambe profile image
Mark Lambe

Hey! Yeah you're absolutely right and I figured the same thing about changing drives so I only mentioned it as a Mac thing. Also true about the reinstalling but I don't think there's anything we can do about that, though PRs are always welcome πŸ˜€

Collapse
 
moopet profile image
Ben Sinclair

Mmm, I've been looking at this. My mac doesn't have the same output from smartctl as you, so it tells me it's been on for 0 hours.
I see it formatted like this:

Power On Hours: 201

Fiddling with knobs has given me the following which might work for most systems:

smartctl -a $(mount | awk '{if ($3 == "/") print $1}') | awk '{if ($2 == "Power_On_Hours") print $4; if ($3 == "Hours:") print $4}'

I'm using mount to get the drive associated with the root file system (which it likely to be the oldest drive in the machine), and then checking for both variants of label.

smartctl needs to be run as root on Linux, so something like this could handle both without unnecessarily elevating privileges on MacOS:

let smartctl = 'smartctl';

if (process.platform == 'linux') {
  smartctl = 'sudo smartctl';
}

I'm not going to build a PR because frankly I have no idea how this will work on other people's environments!

Thread Thread
 
moopet profile image
Ben Sinclair

I made it work with linux on my machine, relying on tune2fs which will only work with ext3/4 filesystems, and I get this:

Since you turned on this machine (4 Aug 2019, 141 days ago) it's been on for 16305 hours, an average of 115 hours and 38 minutes per day.

Which is perhaps unhelpful...