[PLUG] perl reading /proc/loadavg

Randy.Dunlap rddunlap at osdl.org
Thu Mar 21 23:20:32 UTC 2002


On Thu, 21 Mar 2002, Paul Heinlein wrote:

| On Thu, 21 Mar 2002, Randy.Dunlap wrote:
|
| >
| > 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.  :(
|
| I think you'll need to put your open() call in the subroutine:
|
|   sub do_loadavg {
|     open( LOADAVG, '/proc/loadavg' ) or die $!;
|     chomp( my $loadavg = <LOADAVG> );
|     die "error reading loadavg" unless defined( $loadavg );
|     print "load[1, 5, 15], rdyq/ttl_procs, last_pid:  $loadavg\n";
|     close( LOADAVG ) or die $!;
|   }
|
| although I'm not really sure why that is :-(

Yes, I'm aware that I could have done it this way,
but it makes no sense to me to have to do this....

I'll futz around with it some more.

-- 
~Randy





More information about the PLUG mailing list