tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
From: Garry Optland <Garry.Optland () COM ! PACPOWER ! nswgovpower ! telememo ! au> (Tel)
To: tru64-unix-managers
Subject: SUMMARY: Uptime
Date: Thu, 21 Dec 1995 09:38:39 +0000	[thread overview]
Message-ID: <96075325906735.tru64-unix-managers.marc@archive.invalid> (raw)

   Hi All,
   
   My question was:
   
   > Does anybody know of an easy way to get the system uptime from a shell
   > script? (or a small C program, if the source is provided ;-) 
   >
   > I want to be able to output the days, hours and minutes that the
   > system has been up. The output of the uptime command is very variable,
   > and therefore hard to format. 
   >
   > I could not find any info on how the system determines its uptime.
   >
   > All answers will be summarised.
   >
   
   First of all, I will just clarify what I meant by "The output of the 
   uptime command is very variable" 
   
   The problem with uptime is that it is very smart with its formatting. 
   The format is one of the following,
   
   xx.yy up 4 mins, 1 user...
   xx.yy up 1 hr, z users...
   xx.yy up 1 day, z users...
   xx.yy up 1 day, 46 mins, z users...
   xx.yy up 1 day, 1hr, z users...
   xx.yy up 1 day, 1:01, z users...
   xx.yy up 19 days, z users...
   xx.yy up 19 days, 2 hrs, z users...
   xx.yy up 19 days, 2:02, z users...
   
   Who knows what happens if your system is up for a year or more.....
   
   There are just too many variables to easily extract the uptime only, 
   without the leading time and number of users etc. I thought that if I 
   could determine the seconds since booting, then I could do whatever 
   formatting I require.
   
   I got several leads, all of them very useful. 
   
   1. who -r
   2. Read the man pages on table(2)
   3. A perl script that converts the output from uptime in its various 
   formats to a time in seconds. While I haven't tested this script on all 
   the different outputs from uptime, it works on all the ones I have 
   tried. I have included the script below.
   
   Thanks to the following for answers:
   
   alan@nabeth.cxo.dec.com
   lwm@uvo.dec.com
   Knut Hellenbo
   Anthony Jackson
   Steffen Kluge
   Todd Kover
   Paul A Sand
   Sean Watson
   
   
   #!/usr/bin/perl
   open(UP, "uptime|") || die "Ack! Can't run uptime: $!\n";
   $upstr = <UP>;
   close(UP);
   $upstr =~ s/^.*\s+up\s+//;
   $upstr =~ s/,\s+(\d+) user.*//;
   $up = 0;
   ($upstr =~ /(\d+) days?/) && ($up += 86400 * $1);
   ($upstr =~ /(\d+) hrs?/)  && ($up += 3600 * $1);
   ($upstr =~ /(\d+) mins?/) && ($up += 60 * $1);
   ($upstr =~ /(\d+):(\d+)/) && ($up += 3600 * $1 + 60 * $2);
   print "Up for $up seconds\n";
   
   
   regards,
       -Garry.
        <garry.optland@com.pacpower.nswgovpower.telememo.au>

                 reply	other threads:[~1995-12-21  9:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox