Php Exec - System RAM
I have been searching for a couple of days, and I know what needs to be done, but cannot figure out the exact code. I know it has to use preg_match in order for me to get exactly what I want, but I am having trouble with that.
I am trying to have a script show how much RAM is used on the system. I am using php exec. I use the command free -mt. Online, I did manage to find this code and I made a slight modification.
exec('free -mo', $out);
var_dump($out[1]);
list($mem, $used, $free, $shared, $buffers, $cached) = explode(" ", $out[1]);
echo "Memory: " .$used. "/" . $total;
What do I need to do if I just want to output the amount of RAM currently used?
The code ends of showing all RAM, but I need it to just show the total used amount of RAM. My output I get from that code is:
string(73) "Mem: 768 162 605 0 0 0" Memory: /
I would like it to just say the RAM being used, so, it should come out saying 162 during this exact moment, 开发者_如何学Pythonbut it should be real data every time it is refreshed.
The reason the above code is not working is because you're exploding on a space while the value contains many spaces in between the values you're trying to get. A better practice is to use a preg_match_all().
Try this instead:
exec('free -mo', $out);
preg_match_all('/\s+([0-9]+)/', $out[1], $matches);
list($total, $used, $free, $shared, $buffers, $cached) = $matches[1];
echo "Memory: " . $used . "/" . $total;
精彩评论