Perl – system load

To find the system load use the following perl snippet :

1) System load of last one minute :

my $system_load = exec('uptime | awk -F "load average: " \'{ print $2 }\' | cut -d, -f1');
my $system_load = qx('uptime | awk -F "load average: " \'{ print $2 }\' | cut -d, -f1');

2) 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');

3) 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');

5 thoughts on “Perl – system load

  1. Sorry but this is plain wrong. exec replaces the perl process and does not return. Your variables will never be used and will never hold any of the values in them.

    The examples might print the correct values to the screen but this has nothing to do with perl.

    If you need the output of a shell command use backticks or qx().

  2. you don’t need the single quotes in the qx(‘ – should just be qx(uptime…
    and you need to escape $2

  3. Pingback: DBA_MYSQL_LINUX_Perl » Perl取CPU的值得进程列表

Leave a Reply