12

Something similar to linux

cat /proc/uptime

which returns the uptime in seconds, and preferably not parsing uptime(1).

Mark Harrison
  • 283,715
  • 120
  • 322
  • 449
  • please see: https://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds/48762631#4876263 – splaisan Feb 14 '18 at 13:00

7 Answers7

17

The Uptime article on Wikipedia has an interesting lead:

Using sysctl

There is also a method of using sysctl to call the system's last boot time: $ sysctl kern.boottime kern.boottime: { sec = 1271934886, usec = 667779 } Thu Apr 22 12:14:46 2010

Which references sysctl(8), which references sysctl(3).

Community
  • 1
  • 1
Shaggy Frog
  • 27,376
  • 16
  • 87
  • 128
16

Old question, I know, but I needed to do the same thing so I thought I'd post the code I'm using, which I got from http://cocoadev.com/wiki/FindingUptime

#include <time.h>
#include <errno.h>
#include <sys/sysctl.h>

double uptime()
{
    struct timeval boottime;
    size_t len = sizeof(boottime);
    int mib[2] = { CTL_KERN, KERN_BOOTTIME };
    if( sysctl(mib, 2, &boottime, &len, NULL, 0) < 0 )
    {
        return -1.0;
    }
    time_t bsec = boottime.tv_sec, csec = time(NULL);

    return difftime(csec, bsec);
}
Bleyddyn
  • 338
  • 3
  • 12
2

If anyone is trying to do this programmatically using sysctl.h and is expecting a string back like what you see in the command line, the returned value that I get is a 16 byte array, not a string:

sysctlbyname("kern.boottime", value, &size, NULL, 0);

An example for what gets put into value in hex starting from the [0] index:

a9 af c6 4e 0 0 0 0 0 0 0 0 28 be 92 55

The first 4 bytes (maybe the first 8, won't know until Jan 2012) is the epoch time in little endian byte order.

Jon
  • 1,299
  • 1
  • 11
  • 30
1

correct way:

CFTimeInterval getSystemUptime(void)
{
    enum { NANOSECONDS_IN_SEC = 1000 * 1000 * 1000 };
    static double multiply = 0;
    if (multiply == 0)
    {
        mach_timebase_info_data_t s_timebase_info;
        kern_return_t result = mach_timebase_info(&s_timebase_info);
        assert(result == noErr);
        // multiply to get value in the nano seconds
        multiply = (double)s_timebase_info.numer / (double)s_timebase_info.denom;
        // multiply to get value in the seconds
        multiply /= NANOSECONDS_IN_SEC;
    }
    return mach_absolute_time() * multiply;
}

also you could use CACurrentMediaTime() from QuartzCore.framework (which has same code probably) - Available since OS X v10.5 and iOS 2.0

Maxim Kholyavkin
  • 4,254
  • 2
  • 36
  • 78
  • I suspect this is a better solution. I would also suggest using `mach_continuous_time()` (in place of `mach_absolute_time()`) if running macOS 10.12 or later. – James Bucanek May 16 '18 at 00:46
0

There is a function UpTime declared in DriverServices.h. I believe this is equivalent to another function mach_absolute_time. Both seem to be undocumented.

JWWalker
  • 21,925
  • 5
  • 56
  • 74
0

Unfortunately the "sysctl kern.boottime" returns the seconds of the timestamp, not elapsed seconds.. Multiple calls do not increase the second count, but must be seconds from epoc of the boot date itself.

Uri
  • 1
  • 2
    Just subtract the system's boot time from the current time to find out how long the system has been up. – Gabe Jul 20 '10 at 22:48
  • 2
    @Gabe - which gives you wrong results if the system time has been modified since bootup. – lImbus Jan 05 '14 at 15:45
  • FYI after trial and error, something like this solved my issue (slightly different than OP) date -r `sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//'` "+%Y%m%d%H%M%S" – JWHardcastle Aug 31 '14 at 13:21
  • please have a look to https://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds/48762631#4876263 – splaisan Feb 14 '18 at 13:01
0

A simple Lua script to do exactly what you ask for:

local now=tonumber(io.popen("date +%s"):read())
local boottime=tonumber(io.popen("sysctl -n kern.boottime"):read():match("sec = (%d+)"))
local uptime=now-boottime
Valerio Schiavoni
  • 1,273
  • 1
  • 12
  • 19