7

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

JF.
  • 81
  • 1
  • 4
  • I'm not a big Sun user, but I think there is some means to get the boot time in epoch format, which easily gives you what you want. – Tim Post Nov 02 '09 at 12:31

9 Answers9

5

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/

vulcan raven
  • 32,612
  • 11
  • 57
  • 93
Andre Miller
  • 15,255
  • 6
  • 55
  • 53
4

This is a mix of some answers already given. Outputs uptime in seconds with only two commands. Tested on Solaris 9 and 10.

kstat -p unix:0:system_misc:boot_time | nawk '{printf "%d\n", srand()-$2}'
3

A rather unorthodox method might be this (helped me out since the kstat yielded some weired results that did not pass cron:

RunTime=""
RunTime=`kstat -p unix:0:system_misc:snaptime | awk '{print $2}' | cut -d "." -f1`
echo $RunTime
RunTime=`expr $RunTime / 1`
RunDays=`expr $RunTime / 86400`
RunHours=`expr $RunTime % 86400 / 3600`
RunMinutes=`expr $RunTime % 3600 / 60`
RunSeconds=`expr $RunTime % 60`

Hope it helps you out - Nice side effect: You have the time bits available for calculations.

Gerhard
  • 31
  • 1
  • 1
    Using this in a script, the print $2 got confused with my options. instead i used `kstat -p unix:0:system_misc:snaptime | cut -f2 | cut -d "." -f1` – andrew lorien Oct 11 '13 at 04:05
  • This is inaccurate on a zone - it returns the uptime of the global. – Nick May 19 '15 at 13:40
1

Thanks to Andre for a solution that will provide seconds. If anyone is looking for an answer without compiling, this script can be used. Note, as the "uptime" command does not provide seconds the solution is anything from 0 to 59 seconds out when it is run:

days=`uptime | awk '{print \$3}'`
hrs=`uptime | awk '{print \$5}' | sed 's/[:,]/ /g' | awk '{print \$1}'`
mins=`uptime | awk '{print \$5}' | sed 's/[:,]/ /g' | awk '{print \$2}'`
uptimesecs=$(($mins*60))
uptimesecs=$(($hrs*3600+$uptimesecs))
uptimesecs=$(($days*86400+$uptimesecs))
echo "$uptimesecs seconds uptime (to within 59 secs)."

Hope that's of use to someone :-)

JF.
  • 81
  • 1
  • 4
  • I think this is not a good idea as you are running uptime command 3 times to find days, hrs and mins. I would suggest to use kstat instead of uptime. – Space Nov 06 '09 at 10:37
  • I've looked at kstat & it doesn't output any uptime that I can see. How were you thinking of using it to get the uptime? Certainly the script can be improved by storing the output of uptime in a variable then extracting the data from that variable. I'll leave that as an exercise for the reader though :-) – JF. Nov 11 '09 at 09:33
0

Use truss to extract the creation time of the directory /proc/0. (Must be run as root.)

#!/bin/bash

btime=$(truss -v lstat -t lstat ls -ld /proc/0 2>&1 | awk '/ct = /{print $9}' | cut -d. -f1)
0

Perl: CPAN provides unix::uptime - however, it is not currently compatible with SunOS but may be in the future.

JF.
  • 81
  • 1
  • 4
0

Here is a shell script that provides second resolution. Note that you need to be root for it to work.

#!/bin/ksh
now=$(nawk 'BEGIN{print srand()}')
i=$(apptrace -fv 'getutxent' uptime 2>&1 | grep time_t | tail +2 | head -1 | nawk '{print $3}')
ut=$(bc <<-%EOF%
ibase=16
$(print $i | sed 's/0x//' | tr "[a-f]" "[A-F]")
%EOF%
)
s=$((now-ut))
h=$((s/3600))
s=$((s-(h*3600)))
m=$((s/60))
s=$((s-(m*60)))
d=$((h/24))
h=$((h-(d*24)))
printf "%d seconds - %d day(s) %02d:%02d:%02d\n" $((now - ut)) $d $h $m $s
jlliagre
  • 29,783
  • 6
  • 61
  • 72
0

You can use kstat to find the system uptime.

$kstat -p unix:0:system_misc:boot_time

This will return the values in unix format. Below is the code which was very useful for me to get the values in sec.

#!/usr/bin/perl -w
use strict;

my $KSTAT = '/usr/bin/kstat -p';
my $STATISTIC = 'unix:0:system_misc:boot_time';

my $uptime = `$KSTAT $STATISTIC | awk '{print \$2}'`;
sprintf "%0.2f\n", (time() - $uptime);
Space
  • 7,049
  • 6
  • 49
  • 68
0

Use truss on the date command to get epoch time and subtract boot time obtained from kstat.

expr `truss date 2>&1 |grep '^time()' |tr -cd "[0-9]"` - `kstat -p unix:0:system_misc:boot_time|cut -f2`
David
  • 1