2

I have a simple php script 'top.php' with shell_exec function.

Code:

<?php
echo shell_exec("top");
?>

What I am looking for is to view results of 'top' command on a web browser. So, if I access http://192.168.1.1/top.php I want to see results of top command. It is essential to view results of top command periodically as you would see in a command line terminal.

However, when I access 'http://192.168.1.1/top.php' on web browser, it does not display anything. Same behaviour when I execute top.php on command line (as 'php top.php').

I am not sure what or where it is going wrong.....

SamB
  • 23
  • 1
  • 4
  • could you do `var_dump(shell_exec("top"));` ? That way we'll know more like for example the return type – Florian Humblot Jun 20 '17 at 12:19
  • Perhaps `top` isn't exiting and returning output? If I remember correctly, you might be able to do something like `top -n 1` or even `top -n 1 -b` to just get one page of output and exit. – David Jun 20 '17 at 12:21
  • [This post](https://stackoverflow.com/questions/20107147#20109859) should answer your question (: – Lars Beck Jun 20 '17 at 12:22
  • @FMashiro, var_dump(shell_exec("top")); produces same behaviour, i.e. script hangs, no output. – SamB Jun 20 '17 at 12:26
  • Well, script hangs is pretty different from having no output. If the page doesn't load, you may have to check the PHP logs on the server. – Florian Humblot Jun 20 '17 at 12:27
  • @David, Thanks, 'top -n 1' does show results of top command. Is there any way I can loop it endlessly? I need to see top command results on web browser periodically, say every 3 seconds. – SamB Jun 20 '17 at 12:28
  • @FMashiro, Sorry for typo. I meant, script does not produce any output. – SamB Jun 20 '17 at 12:32
  • I managed to progress few steps since yesterday. Got a new script that parses top command output (originally described at https://stackoverflow.com/questions/22789444/parsing-output-of-top-command-shell-with-php). Code of new script: '; echo '
    ';
        echo $a;
        echo '
    '; echo ''; ?>
    – SamB Jun 21 '17 at 12:04
  • However, /tmp/4.4.txt is created as empty file (file permission issue?), but isn't /tmp writable by all users? If I run php on command-line as root, 4.4.txt captures output of top command, and now if I access same php on browser I can see contents of 4.4.txt file. How to resolve this file permission/creation issue? It appears php script is executing fine once 4.4.txt file exists. Any alternative ideas? – SamB Jun 21 '17 at 12:10

1 Answers1

2

top on the command line by default just keeps running, so I suspect what's happening here is that it's not exiting and returning output to PHP. The -n 1 flag should address that:

<?php
echo shell_exec("top -n 1");
?>

This would give you one "page" of output from top to display on your webpage. In order to refresh it, you would of course just refresh the webpage.

To make something a little smoother (where you don't have to refresh the page), you could have a page which makes an AJAX request to this PHP script and then displays the output on the page. That AJAX request could then be scheduled with setInterval() in JavaScript to occur ever X seconds as you see fit.

David
  • 208,112
  • 36
  • 198
  • 279
  • Good god, and I thought being a PHP novice is enough to get the job done ! :) Thanks for your detailed response, I will start looking into Ajax, JScript books. – SamB Jun 20 '17 at 12:35
  • 3
    If you want to display output on the browser by AJAX call, use the command as `top -b -n 1` – Pramod Sep 06 '20 at 19:49