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.

Ryan
  • 375
  • 1
  • 3
  • 15

4 Answers4

7

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

Ryan
  • 375
  • 1
  • 3
  • 15
  • 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"` – Haravikk Sep 25 '14 at 10:20
  • 4
    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`. – Jibran Dec 25 '15 at 12:30
  • 1
    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));` – carpii Dec 31 '20 at 21:14
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
Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 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. – Ryan Mar 11 '13 at 01:01
  • 1
    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. – Michael Dautermann Mar 11 '13 at 01:04
5
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

ngj
  • 883
  • 7
  • 17
ZedicusZZ
  • 71
  • 1
  • 1
  • I'm writing a shell script for macOS and need to get uptime in seconds. This is really helpful. – Tong Mar 06 '19 at 14:49
0

Here is what I Do to get the the values instead of Cut method

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

Where $5 is the Range of the string

Example

$1 Gives you "sysctl kern.boottime"
$2 Gives you "{"
$3 Gives you "sec"

from the String

kern.boottime: { sec = 1604030189, usec = 263821 } Fri Oct 30 09:26:29 2020
iOSDeveloper
  • 461
  • 4
  • 20