[PLUG] perl reading /proc/loadavg
Curtis Poe
cp at onsitetech.com
Thu Mar 21 23:21:01 UTC 2002
----- Original Message -----
From: "Randy.Dunlap" <rddunlap at osdl.org>
To: <plug at lists.pdxlinux.org>
Sent: Thursday, March 21, 2002 2:29 PM
Subject: [PLUG] perl reading /proc/loadavg
>
> If I read /proc/loadavg like this (Perl, a la C) (called from a loop):
>
> sub do_loadavg
> {
> seek (LOADAVG, 0, 0) or die "error seeking loadavg";
> $loadavg = "nil";
> $loadavg = <LOADAVG>; # 1 line
> die "error reading loadavg" if !defined($loadavg);
> chomp $loadavg;
> print "load[1, 5, 15], rdyq/ttl_procs, last_pid: $loadavg\n";
> } # end do_loadavg
>
> I don't see the data changing. :(
Randy,
There is nothing fundamentally wrong with the snippet you sent, as far as I
can tell. I ran the following snippet just fine.
use strict;
my $file = "/proc/loadavg";
open LOADAVG, "< $file" or die "Cannot open $file: $!\n";
while(1)
{
print_loadavg( \*LOADAVG );
sleep 5;
}
sub print_loadavg
{
my $LOADAVG = shift;
seek ($LOADAVG, 0, 0) or die "error seeking loadavg: $!";
chomp ( my $loadavg = <$LOADAVG> );
die "error reading loadavg" if ! defined $loadavg;
print "load[1, 5, 15], rdyq/ttl_procs, last_pid: $loadavg\n";
}
Caveat: since I am using a Windows box here at work, I ran this on a
different file and manually changed the first line of the file while this
was reading. However, I can't imagine how this would make a difference.
Cheers,
Curtis "Ovid" Poe
--
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push at A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift at a;shift at a if $a[$[]eq$[;$_=join q||, at a};print $_,$/for reverse @A
More information about the PLUG
mailing list