On 03 Oct 2002 09:25:03 -0400, [EMAIL PROTECTED] (Chad Kellerman) wrote: >Hi everyone, > > What would be the easiest way to find the uptime of a linux >machine? I don't want to use a system call. Is there a module I can >use? Or do I have to open /proc/uptime and calculate it thru there?
Here's a uptime script which runs nice: #################################################################3 #!/usr/bin/perl -w use strict; open UPTIME, "/proc/uptime" or die "cannot open '/proc/uptime'.\n"; my $time = <UPTIME>; close UPTIME; $time = sprintf "%.0f", $time =~ /^(\d+)\./; print time2string($time), "\n"; ######################################################### sub time2string { my $time = shift; return if ! defined $time or $time !~ /^\-?\d+$/; my $prefix; if ($time < 0) { $time = - $time; $prefix = "- "; }else {$prefix='';} my $s = $time % 60; my $m = ($time / 60) % 60; my $h = ($time / (60 * 60)) % 24; my $d = int($time / (24 * 60 * 60)); (my $y, $d) = modulus_thingamajig(365, $d); (my $mo, $d) = modulus_thingamajig(30, $d); (my $w, $d) = modulus_thingamajig(7, $d); my @data; push @data, sprintf "%dy", $y if $y != 0; push @data, sprintf "%dmo", $mo if $mo != 0; push @data, sprintf "%dw", $w if $w != 0; push @data, sprintf "%dd", $d if $d != 0; push @data, sprintf "%dh", $h if $h != 0; push @data, sprintf "%dm", $m if $m != 0; push @data, sprintf "%ds", $s if $s != 0 or ! @data; return $prefix . join ' ', @data; } ################################################################ sub modulus_thingamajig { my ($days, $d) = @_; my $return; if ($d >= $days) { $return = int($d / $days); $d = $d % $days; } else { $return = 0; } return ($return, $d); } ################################################################## -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]