Top command with php does not return the same terminal value

Asked

Viewed 73 times

2

Well I am building a php code to monitor my CPU usage. I am using the top command that is native to linux. I am using a pc with Ubuntu server 18.04 for testing.

Well what happens is that I open the terminal and run php together, the value of the cpu is always higher in the terminal, which is around 20% on average and php is locked in 9.1%.

I’m going to post the code I’m putting together, I wanted to know the reason for the difference and how to fix it.

    // Executa comando para consulta da CPU
exec('top -d 0 -n 5 -b | grep Cpu', $cpu);
preg_match('/\d+\../', $cpu[0], $cpu_us);

// Monta array com os cores do processador
for($i=1; $i < count($cpu);$i++){
    preg_match('/\d+\../', $cpu[$i], $cores_us);
    $cores[] = $cores_us[0];
}



// Monta o array de resposta
$array = array(
    "cpu" => array(
        "geral" => $cpu_us[0],
        "cores" => $cores
    )
);

var_dump($array);

OBS: I realized that whenever I run php the values of the colors are changing, but the general cpu does not change much.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.