这适用于我在 OSX 上:
<?php
exec('ps -e -o %mem,%cpu,command | grep httpd', $output);
$proc_data = [];
foreach($output as $key => $value) {
// Make sure it's only path httpd and not the grep included
if (strstr($value, '/httpd')) {
$info = explode(' ', trim($value), 5);
unset($info[1]);
unset($info[2]);
$proc_data[] = array_merge($info);
}
}
echo '<pre>';
print_r($proc_data);
echo '</pre>';
// Caclulate total CPU percentages
$total_cpu = 0;
foreach ($proc_data as $key => $value) {
$total_cpu += $value[1];
}
echo $total_cpu;
?>
这是 bash 的终端输出:
MacBook-Pro:~ user$ ps -e -o %mem,%cpu,command | grep httpd
0,2 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,1 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,1 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 9,0 /Applications/MAMP/Library/bin/httpd -k start
0,0 0,0 grep httpd
我注意到ps -e -o %mem,%cpu,cmd
必须是command
,但它可能是仅 OSX 的东西。希望你能用这个工作。
祝你好运!