phplinuxpassthrusystem-information

Get certain information from passthru and top


I have tried to use this possible solution without any luck:

$test = passthru('/usr/bin/top -b -n 1');
preg_match('([0-9]+ total)', $test, $matches);
var_dump($matches);

That code shows the following text:

top - 19:15:43 up 31 days, 23 min, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 85 total, 1 running, 84 sleeping, 0 stopped, 0 zombie Cpu(s): 0.1%us, 0.0%sy, 0.0%ni, 99.8%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 247980k total, 224320k used, 23660k free, 68428k buffers Swap: 521212k total, 37120k used, 484092k free [...] 0.4 0:00.00 top array(0) { }

How can I take a certain information like total of tasks so it only show for example 84 total?

Thanks in advance.


Solution

  • I got it to work thanks to Barmar and Satish. Here's the correct code:

    exec("/usr/bin/top -b -n 1 | grep Tasks: | awk -F' ' '{print $4}'")

    Many thanks! :)