Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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
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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top