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.

7
  • 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 4, 2013 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 4, 2013 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 4, 2013 at 18:28
  • 1
    You need to escape the embedded double quotes so they don't end the PHP string.
    – Barmar
    Feb 4, 2013 at 18:44
  • 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 4, 2013 at 18:46

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.