phpshellhtop

Run shell commands with imaginative display


I am trying to output htop output on my server as a viewable HTML page so I can check on it easily. I got the export to HTML part done already (using echo q | htop | aha --black --line-fix). This works perfectly fine when I'm using it interactively, and now what I needed to do is to connect it to a web portal. My setup is PHP, and I use exec() and shell_exec() to try to display the output. However, the output is blank. I suspect this is because there is no display that htop knows about, so it has no way of knowing what the correct dimension to display is. So my question is, is there a way to tell a generic process/shell script that it can use an imaginative display with specified dimensions?


Solution

  • I got it. The easiest way to do this is not using htop anymore. The author said that htop is supposed to be used interactively, and that there is no easy way to get the output of it. Another monitoring tool with a built-in web server mode is glances, and you can do something like glances -w and you are all set.

    However, if you really want to do htop then I got that figured out. Simplest working code is:

    echo shell_exec("export TERM=xterm-color;echo q | htop | aha --black --line-fix");
    

    More customizable working code is:

    exec("rm /var/www/html/abc");
    exec("export TERM=xterm-color;echo Hthq | htop | aha --black --line-fix > /var/www/html/abc");
    echo shell_exec("cat /var/www/html/abc | head -n 10");
    echo shell_exec("cat /var/www/html/abc | tail -n 25");
    

    This allows you to change settings around in the echo Hthq part. The final hq opens htop's help menu and then closes it, forces htop to redraw everything. The head and tail bits will cut out the correct segments. Everything before (like Ht, which hides threads and display tree view) you can customize.