0

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.

Airikr
  • 6,258
  • 15
  • 59
  • 110
  • 1
    `passthru()` doesn't return the output, it sends it to the browser. If you want to capture the output in a variable, use `exec()`. – Barmar Feb 04 '13 at 18:23
  • 1
    Just wonder if you try this: /usr/bin/top -b -n 1 | grep Tasks: | awk -F' ' '{print $2 " " $3}' – Satish Feb 04 '13 at 18:26
  • Barmat: Thanks for your answer but `exec()` gives me no output at all. Satish: Nope but I tested it now and it gives me error 500 (Internal Server Error); `passthru("/usr/bin/top -b -n 1 | grep Tasks: | awk -F' ' '{print $2 " " $3}'")`. I'm getting the same error if I use `exec()` instead of `passthru()` with that command – Airikr Feb 04 '13 at 18:28
  • @ErikEdgren You need to put "@" before names to notify us of replies, and also spell our names correctly (when you type @ at the beginning of a comment, it auto-completes to assist with that). – Barmar Feb 04 '13 at 18:43
  • 1
    You need to escape the embedded double quotes so they don't end the PHP string. – Barmar Feb 04 '13 at 18:44
  • @Barmar The R is near the T and it happened to be a T. Sorry :) I got it working now though with the solution you just said :) – Airikr Feb 04 '13 at 18:45
  • 1
    `exec()` only returns the last line of the output. If you want to look at other lines, you have to pass the optional second argument. – Barmar Feb 04 '13 at 18:46

1 Answers1

0

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! :)

Airikr
  • 6,258
  • 15
  • 59
  • 110