3

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)

hakre
  • 193,403
  • 52
  • 435
  • 836
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
  • http://www.webdeveloper.com/forum/showthread.php?256317-how-to-capture-output-from-interactive-program – hakre Mar 29 '13 at 13:18
  • Im digging for the same. When you use a shell, you can see the stream on the remote ssh client, so, i think that would not to be impossible using a SSH lib, like libSSH2 or phpseclib (purephp ssh implementation). I know that PHP can flush a stream from any connection (that's how a video works... as example) but i didnt' find yet the way to do so because im really new to the use of libSSH2. Very interesting thing. I ask myself too about the CLI programs with interactive inputs. I like/need a way to get a real ssh feel from a web. – m3nda Apr 13 '15 at 02:19

4 Answers4

3

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; 
hakre
  • 193,403
  • 52
  • 435
  • 836
d g
  • 1,594
  • 13
  • 13
  • so there's no way i can just stream the iterations to a file continuously ? – Rami Dabain Mar 22 '13 at 05:44
  • If you want to display this info on a webpage continuously, you could probably artificially do it by calling the "top -n 1" command via AJAX in a loop, and overwrite a div with the newest data on every iteration. At anytime you could run this in a cron, or infinite loop, which overwrites a file. – d g Mar 22 '13 at 08:06
  • 1
    actually i want it for `slurm` and `htop` for the detailed CPU usage and tree-based PIDs – Rami Dabain Mar 22 '13 at 14:34
  • @RonanDejhero: Talk to a linux sysadmin, she should be able to name you a few alternative programs that are non-interactive that provide what you need if you explain in detail your needs in a discussion with her. – hakre Mar 29 '13 at 12:17
2

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>
kjagiello
  • 8,269
  • 2
  • 31
  • 47
spabam
  • 21
  • 1
1

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');
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • I will be using websock not Ajax , and i need data from htop and slurm not top . thank you . – Rami Dabain Apr 03 '13 at 02:22
  • Than use web socket requests to update the output of htop. It makes no difference. What are you expecting? the colorized output of htop on a webpage? this will not work (as you may expect) – hek2mgl Apr 03 '13 at 02:28
  • `htop` seems to be poor coded as it doesn't respect the glibc function `isatty()` and therefore will not suite that easily with php (although there are *hacky* ways to achieve this). – hek2mgl Apr 03 '13 at 03:09
0

system and exec return the results of the command as a string. Use fwrite or a similar function to save the output to a file.

blakeo_x
  • 495
  • 5
  • 14