Top command with php does not return the same value as the terminal

2

I'm building a code in php to monitor my CPU usage. I'm 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 the php together, the value of the cpu always greater in the terminal, which is on average around 20% and php gets 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 solve 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);

    
asked by anonymous 19.12.2018 / 13:39

0 answers