Getting the uptime of a SunOS UNIX box in seconds only

JF. picture JF. · Nov 2, 2009 · Viewed 8k times · Source

How do I determine the uptime on a SunOS UNIX box in seconds only?

On Linux, I could simply cat /proc/uptime & take the first argument:

cat /proc/uptime | awk '{print $1}'

I'm trying to do the same on a SunOS UNIX box, but there is no /proc/uptime. There is an uptime command which presents the following output:

$ uptime
12:13pm  up 227 day(s), 15:14,  1 user,  load average: 0.05, 0.05, 0.05

I don't really want to have to write code to convert the date into seconds only & I'm sure someone must have had this requirement before but I have been unable to find anything on the internet.

Can anyone tell me how to get the uptime in just seconds?

TIA

Answer

Andre Miller picture Andre Miller · Nov 2, 2009

If you don't mind compiling a small C program, you could use:

#include <stdio.h>
#include <string.h>
#include <utmpx.h>

int main()
{
  int nBootTime = 0;
  int nCurrentTime = time ( NULL );
  struct utmpx * ent;

  while ( ( ent = getutxent ( ) ) ) {
    if ( !strcmp ( "system boot", ent->ut_line ) ) {
      nBootTime = ent->ut_tv.tv_sec;
    }
  }

  printf ( "System was booted %d seconds ago\n", nCurrentTime - nBootTime );
  endutxent();

  return 0;
}

Source: http://xaxxon.slackworks.com/rsapi/