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_at_nabeth.cxo.dec.com
lwm_at_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_at_com.pacpower.nswgovpower.telememo.au>
Received on Thu Dec 21 1995 - 00:09:36 NZDT