Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Total System Uptime

Is there a way to view total uptime (as in, since the OS was installed)? I'm curious about it.

Even if not built in, does anyone know of an app or script which can keep a running total? Thanks.

PowerBook G4 (15-inch Double-Layer SD), Mac OS X (10.4.6), 1.5 GB RAM, iPod 3G 20GB

Posted on May 27, 2006 6:05 PM

Reply
Question marked as Best reply

Posted on May 27, 2006 11:47 PM

Hi Sidney,
The "uptime" utility will tell you the time since the last boot. I assume you know that. If you want to keep a running total yourself, you can write your own script to do that. For instance, the following script should keep the total uptime in seconds minus the current uptime in seconds in a file named .utot. I'm assuming below that the script is always run with the root of the user's home directory as the current working directory.

/usr/bin/uptime | /usr/bin/awk '{ sub(/.* up /,""); sub(/, *[^ ]* users./,""); DAYS = $0; HOURS = $0; MIN = $0; sub(/ .*/, "", DAYS); sub(/.*, */,"", HOURS); sub(/:.*/, "", HOURS); sub(/.*:/, "", MIN); SEC = ( DAYS 24*6060 + HOURS 6060 + MIN*60 ); getline OLDSEC < " } else { getline TOTSEC < "; TOTSEC = TOTSEC + OLDSEC; print TOTSEC > ".utot"; print SEC > ".utmp" }print "oldsec = " OLDSEC }'

You can instead use absolute paths in the references to files above to allow you to run the script in a cron job. Every time the machine is rebooted, the SEC variable, which measures current uptime in seconds will suddenly be less the contents of the .utmp value, stored in the OLDSEC variable. That is interpreted as the uptime between the last two boots and is added to the contents of the .utot file before it is overwritten in the .utmp file by SEC. If you run the above script every minute, your total uptime should lose about a half a minute with each boot. I leave it as an exercise to create a StartupItem to simply run the script when the machine boots and when it shuts down.

The total uptime in seconds will be the sum of the number in the .utot file and the value of the SEC variable. I also leave it as an exercise to write a script to report that in a user friendly form.
--
Gary
~~~~
Q: What's the difference between a Mac and an Etch-a-Sketch?
A: You don't have to shake the Mac to clear the screen.
6 replies
Question marked as Best reply

May 27, 2006 11:47 PM in response to Sidney San Martín

Hi Sidney,
The "uptime" utility will tell you the time since the last boot. I assume you know that. If you want to keep a running total yourself, you can write your own script to do that. For instance, the following script should keep the total uptime in seconds minus the current uptime in seconds in a file named .utot. I'm assuming below that the script is always run with the root of the user's home directory as the current working directory.

/usr/bin/uptime | /usr/bin/awk '{ sub(/.* up /,""); sub(/, *[^ ]* users./,""); DAYS = $0; HOURS = $0; MIN = $0; sub(/ .*/, "", DAYS); sub(/.*, */,"", HOURS); sub(/:.*/, "", HOURS); sub(/.*:/, "", MIN); SEC = ( DAYS 24*6060 + HOURS 6060 + MIN*60 ); getline OLDSEC < " } else { getline TOTSEC < "; TOTSEC = TOTSEC + OLDSEC; print TOTSEC > ".utot"; print SEC > ".utmp" }print "oldsec = " OLDSEC }'

You can instead use absolute paths in the references to files above to allow you to run the script in a cron job. Every time the machine is rebooted, the SEC variable, which measures current uptime in seconds will suddenly be less the contents of the .utmp value, stored in the OLDSEC variable. That is interpreted as the uptime between the last two boots and is added to the contents of the .utot file before it is overwritten in the .utmp file by SEC. If you run the above script every minute, your total uptime should lose about a half a minute with each boot. I leave it as an exercise to create a StartupItem to simply run the script when the machine boots and when it shuts down.

The total uptime in seconds will be the sum of the number in the .utot file and the value of the SEC variable. I also leave it as an exercise to write a script to report that in a user friendly form.
--
Gary
~~~~
Q: What's the difference between a Mac and an Etch-a-Sketch?
A: You don't have to shake the Mac to clear the screen.

May 28, 2006 9:01 AM in response to Gary Kerbaugh

This is great, as are all Gary's scripts, but it is sort of like putting an odometer on your bicycle 5 years after you buy it.

I determine uptime by calculating the number of days between that on the sales receipt and today, and then to be conservative I subtract a day. Mine are always on, and I deprive them not only of sleep but I don't even let them spin their disks down.

(Lots of people like to brag about their time since last reboot. My SGIs could run of a year or more without a reboot, but they were hideously out of date, full of security holes, etc. If you haven't rebooted, you haven't done the security and software updates. A far more relevant statistic would be how many times have you had to reboot due to a system freezeup or crash, but this is harder to tabulate.)

May 28, 2006 10:19 AM in response to Bill Scott

Hi Bill,

> sort of like putting an odometer on your bicycle
5 years after you buy it.


As usual you're way off. Putting an odometer on a 5 year old bicycle is a lot more useful. You have to have some evidence to give to the doctor that you've been exercising. It's too bad that tests for mental problems aren't as easy; I simply couldn't resist the logic of the script. I'm not sure if I've ever used files this way from awk.
--
Gary
~~~~
Marta was watching the football game with me when she said, "You know most of these sports are based on the idea of one group protecting its territory from invasion by another group."

"Yeah," I said, trying not to laugh. Girls are funny.
-- Jack Handley, The New Mexican, 1988.

May 28, 2006 12:50 PM in response to Gary Kerbaugh

Thanks, Gary, this was just what I was looking for. I hope to some day be nearly as experienced with shell scripting. I'll be using the excellent SleepWatcher to call it at startup and shutdown.

As for Bill, I don't get nearly as much out of my bicycle as my computer at the moment, so it probably wouldn't be a good idea to keep a record for my doctor. Also, as this a laptop, it can't be on constantly and I need a different system to monitor uptime.

May 29, 2006 2:25 AM in response to Sidney San Martín

Hi Sidney,

The following command

last reboot shutdown

will show the boot/shutdown history recorded in /var/log/wtmp.
If there are files like wtmp.0.gz in /var/log/, then they are (compressed) old wtmp files. The history in these old wtmp files can be shown by

gunzip -c /var/log/wtmp.0.gz > wtmp.0
last -f wtmp.0 reboot shutdown

It will be not difficult to write a script to calculate the total uptime from the output of the last command.

PowerMac G4 Mac OS X (10.4.5)

May 31, 2006 7:48 AM in response to Bill Scott

> "Lots of people like to brag about their time since last reboot"

D ämn right! Check this out:
<pre>
# Uptime | System Boot up
----------------------------+-------------------------------------------------
-> 1 139 days, 17:08:23 | Darwin 8.4.0 Wed Jan 11 21:14:09 2006
2 139 days, 17:07:53 | Darwin 8.4.0 Wed Jan 11 21:13:41 2006
3 59 days, 18:49:12 | Darwin 8.2.0 Fri Aug 26 14:16:59 2005
4 44 days, 22:18:00 | Darwin 8.1.0 Tue May 17 09:08:46 2005
5 33 days, 18:17:45 | Darwin 8.2.0 Sat Jul 23 16:09:39 2005
6 29 days, 17:46:30 | Darwin 8.3.0 Tue Nov 1 08:27:27 2005
7 25 days, 02:59:32 | Darwin 7.7.0 Sat Jan 8 14:27:14 2005
8 15 days, 23:42:43 | Darwin 8.3.0 Thu Dec 1 10:25:12 2005
9 15 days, 21:27:24 | Darwin 8.3.0 Mon Dec 26 12:56:17 2005
10 15 days, 16:37:09 | Darwin 7.8.0 Wed Mar 30 11:06:59 2005
----------------------------+-------------------------------------------------
</pre>

(ignore line 2, it's a bug that occasionally pops up, something to do with fast user switching or logging out/in, etc. It'll be gone when I finally have to restart)

Sidney - This is from the uptimed program, which is pretty cool. I had problems compiling it ages ago but sorted it out eventually.

Like you say Bill, I'm stuck on 10.4.4 now until the system crashes, which looks like never with Tiger so far... (I've had a few catastrophic crashes, system freezes, etc. but the thing always recovers if I give it enough time to itself! Only once did I have to ssh in and kill the windowserver process which took it back to the loginwindow and everything was fine). This was all started in the interests of OS testing you understand - OSX was touted as 'uncrashable' and all that - finally it seems to be true.

I'm with you, my machines are always on, although I do let the disks sleep at night. Hopefully it crashes soon because the longer it goes the harder the decision will be...

Total System Uptime

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.