Jump to content

Telling how long a Mac has been running


RobertM

Recommended Posts

Does anyone know if there's a way to tell how long (days, hours, etc.) OS X has been up? I go a long time without rebooting, and sometimes I'd just like to know exactly how long, so I can brag to myself and shun my Windows laptop. :lol: J/K on that last part, but it would still just be fun to know.If there's some way in OS X itself to find out, I have yet to discover what it is. Otherwise, does anyone know of an app that can do this?

Link to comment
Share on other sites

Google is your friend!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*60*60 + HOURS*60*60 + MIN*60 ); getline OLDSEC < ".utmp"; if ( SEC >= OLDSEC ){ print SEC > ".utmp" } else { getline TOTSEC < ".utot"; 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.http://discussions.apple.com/message.jspa?messageID=2426517

Link to comment
Share on other sites

I just open a terminal and type uptime:

neil$ uptime 8:31 up 1:06, 2 users, load averages: 0.28 0.18 0.16
The 1:06 part is the uptime. Linux taught me that one :thumbsup:
Link to comment
Share on other sites

Hmm, somehow my Google searching (hey, look, lawyers, I'm not using the product's name as a verb! :'() failed to return anything about uptime, which I'm assuming must work in Linux, too ... hmm.Thanks! Going on 5 days now, and would've been more but I had to reboot for the 10.4.7 update...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...