Perl – system load


To find the system load use the following perl snippet :

  1. System load of last one minute :
my $system_load = exec('<a class="zem_slink" title="Uptime" rel="wikipedia" href="http://en.wikipedia.org/wiki/Uptime">uptime</a> | awk -F "load average: " \'{ print $2 }\' | cut -d, -f1');
my $system_load = qx('uptime | awk -F "load average: " \'{ print $2 }\' | cut -d, -f1');
  1. System load of last 5 minutes :
my $system_load = exec('uptime | awk -F "load average: " \'{ print $2 }\' | cut -d, -f2');
my $system_load = qx('uptime | awk -F "load average: " \'{ print $2 }\' | cut -d, -f2');
  1. System load of last 15 minutes :
my $system_load = exec('uptime | awk -F "load average: " \'{ print $2 }\' | cut -d, -f3');
my $system_load = qx('uptime | awk -F "load average: " \'{ print $2 }\' | cut -d, -f3');

Perl – How to Read a Text File into a Variable – 6 ways to do it

6 Ways to Read a Text File into a Variable

If you are working with large file(s) you might consider using File::Slurp. It is much fast than the conventional:

{
  local $/=undef;
  open FILE, "myfile" or die "Couldn't open file: $!";
  binmode FILE;
  $string = &lt;FILE>;
  close FILE;
}

{
  local $/=undef;
  open FILE, "myfile" or die "Couldn't open file: $!";
  $string = &lt;FILE>;
  close FILE;
}

open FILE, "myfile" or die "Couldn't open file: $!";
$string = join("", &lt;FILE>);
close FILE;
  
open FILE, "myfile" or die "Couldn't open file: $!";
while (&lt;FILE>){
 $string .= $_;
}
close FILE;

open( FH, "sample.txt") || die("Error: $!\n");
read(FH, $data, 2000);
close FH;

The format for the read function is: