Get SunOS UNIX window runtime in seconds

How to determine uptime in a SunOS UNIX unit in seconds?

On Linux, I could just cat / proc / uptime and accept the first argument:

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

I am trying to do the same in a SunOS UNIX window, but not / proc / uptime. There is an uptime command that presents the following output:

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

I really don't want to write code to convert the date to seconds only, and I'm sure someone should have had this requirement before, but I could not find anything on the Internet.

Can someone tell me how to get uptime in just a few seconds?

TIA

+6
unix shell sunos uptime
source share
9 answers

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

 #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 ); } 

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

+4
source share

Perhaps this is a rather unorthodox method (it helped me since kstat gave some 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 this helps you. Good side effect: you have available bits of time for calculations.

+3
source share

Thanks to Andre for a solution that will provide seconds. If someone is looking for an answer without compilation, this script can be used. Note that since the “Uptime” command does not provide seconds, the solution has a value of 0 to 59 seconds at startup:

 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 someone will like it :-)

+1
source share

This is a mixture of some of the answers already given. Displays 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}' 
+1
source share

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

0
source share

Here is a shell script that provides a second resolution. Please note that for this you need to be root.

 #!/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 "[af]" "[AF]") %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 
0
source share

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 is very useful for me to get the values ​​in seconds.

 #!/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); 
0
source share

Use the farm to retrieve the creation time of the / proc / 0 directory. (Must 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
source share

Use the farm in the date command to get the time in the era and subtract the load 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` 
0
source share

All Articles