using one of those codes
system("htop");
//or
exec('htop');
how to keep the data from htop being written into a file or something (time limit of the script is set to 0 don't worry)
using one of those codes
system("htop");
//or
exec('htop');
how to keep the data from htop being written into a file or something (time limit of the script is set to 0 don't worry)
htop is an interactive program that runs in a loop until exited, and doesn't seem to offer any flags to do a single iteration then exit which makes capturing the output problematic.
Have you considered using top? You can limit it to 1 iteration with -n 1:
$resp = system("top -n 1");
print $resp;
This is another solution, which is to use a virtual terminal and do a screen capture, then close the session. Its a nice hack, but top -n 1 is more efficient in resource use. You need to do yum screen if in redhat/centos. Seems to be included in ubuntu/debian.
<?php
`screen -d -m -S htop_session htop`;
sleep(1);
`screen -p 0 -S htop_session -X hardcopy`;
`screen -p 0 -S htop_session -X quit`;
?>
<pre>
<?php print file_get_contents('hardcopy.0'); ?>
</pre>
I agree with the answer of @d_g, top is a program that is intended to run in a terminal. I would suggest to use AJAX to update the outputs of top frequently.
Furthermore I want to add information about the function passthru()
. The function prints the output of the shell command directly to php's stdout. You won't need an additional print and output is available before the external program has finished. That's important if the command takes long to process. So you could replace:
$resp = system("top -n 1");
print $resp;
by
passthru('top -n 1');