/proc/uptime in Mac OS X
Asked Answered
S

4

9

I need the EXACT same output as Linux's "cat /proc/uptime".

For example, with /proc/uptime, you'd get

1884371.64 38646169.12

but with any Mac alternative, like "uptime", you'd get

20:25 up 20:26, 6 users, load averages: 3.19 2.82 2.76

I need it to be exactly like cat /proc/uptime, but on Mac OS X.

Sou answered 11/3, 2013 at 0:27 Comment(0)
S
8

Got it...

$sysctl -n kern.boottime | cut -c14-18
87988

Then I just converted that to readable format (don't remember how):

1 Days 00:26:28

Sou answered 14/3, 2013 at 1:33 Comment(3)
Since I don't like to simply cut on hard boundaries (in case the layout of the output changes), I opted for the following: while IFS=' =', read -d ',' key value; do eval "boot_time_$key='$value'" 2>/dev/null; done <<< $(sysctl -n kern.boottime | sed 's/^.*{//;s/}.*$/,/'), you can then simply pull the value you need from boot_time_sec, and can convert it with something like date -jf '%s' "$boot_time_sec"Mokas
One small problem with this answer. Your cut is getting characters between fixed positions (from 14 to 18). This worked in your case because your system wasn't up for a long time, but for me this caused problems. I changed the cut command to fix this small issue. sysctl -n kern.boottime | cut -f4 -d' ' | cut -d',' -f1.Sex
If you want the uptime in seconds on Mac, you can use ts_boot=`sysctl -n kern.boottime | cut -d" " -f4 | cut -d"," -f1`; ts_now=`date +%s`; echo $(($ts_now-$ts_boot));Odysseus
B
6

There simply is no "/proc" directory on the Macintosh.

On MacOS, you can do a command like:

sysctl kern.boottime 

and you'll get a response like:

kern.boottime: { sec = 1362633455, usec = 0 } Wed Mar  6 21:17:35 2013
Blockade answered 11/3, 2013 at 1:0 Comment(2)
So I should use 'cut' to remove the first x letters, which would be "kern.boottime: { sec = "? I also need to remove the rest... I'm starting out on this kind of stuff.Sou
there's also "sysctl -n kern.boottime", which gets rid of the "kern.boottime" bit. The boottime variable is a struct, and I can't see an easy way built into the sysctl command to get the variables within the struct.Blockade
H
6
boottime=`sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//g'`
unixtime=`date +%s`
timeAgo=$(($unixtime - $boottime))
uptime=`awk -v time=$timeAgo 'BEGIN { seconds = time % 60; minutes = int(time / 60 % 60); hours = int(time / 60 / 60 % 24); days = int(time / 60 / 60 / 24); printf("%.0f days, %.0f hours, %.0f minutes, %.0f seconds", days, hours, minutes, seconds); exit }'`
echo $uptime

Will return something like 1 Day, 20 hours, 10 minutes, 55 seconds

Hostetter answered 18/7, 2013 at 19:28 Comment(2)
I'm writing a shell script for macOS and need to get uptime in seconds. This is really helpful.Ferraro
I think you wanted $5 on the first line. Is there some form of sysctl that puts the sec value in the 4th field? Also, here's an adaptation of this code (using $5) as a one-liner: sysctl kern.boottime |awk -F'[ ,]' '{"date +%s" | getline now; time = now - $5; seconds = time % 60; minutes = int(time / 60 % 60); hours = int(time / 60 / 60 % 24); days = int(time / 60 / 60 / 24); printf("%.0f days, %.0f hours, %.0f minutes, %.0f seconds", days, hours, minutes, seconds) }'Darlenedarline
C
0

Here is what I do to get the the values instead of the cut method:

sysctl kern.boottime | awk '{print $5}'

Where

  • $1 Gives you kern.boottime
  • $2 Gives you {
  • $3 Gives you sec
  • $4 Gives you =
  • $5 Gives you the epoch seconds (and a trailing comma)

from example string

kern.boottime: { sec = 1604030189, usec = 263821 } Fri Oct 30 09:26:29 2020
Chen answered 3/11, 2020 at 7:43 Comment(1)
Beware the trailing comma. Consider sysctl kern.boottime |awk -F'[ ,]' '{print $5}' or even sysctl kern.boottime |awk -F'[ ,]' '{printf "%f", $5+$9/1e6}' to include μsec precision.Darlenedarline

© 2022 - 2024 — McMap. All rights reserved.