Reputation: 103
Executing unix shell commands using PHP Running shell commands using PHP script Execute a shell command through php and display it in browser? I have already referred to the above links. But, I'm experiencing an issue in displaying the linux shell command process in the browser. My linux command: top -n 1 and wanted to display them using php in the browser..
myscript.php
<?php
$var = shell_exec('top -n 1');
echo "<pre>$var</pre>";
?>
Now, when I refresh my browser, I'm unable to see the output in the browser.
Upvotes: 2
Views: 7192
Reputation: 452
Ok, I see your problem Rana.
There some shell commands in linux needed to be set along with the TERM enviroment variable.
top
is one of them.
In addition -b flag must be used in order to get the result from the output buffer, that in this case is the terminal...
Try this code:
<?php
$var = shell_exec('TERM=xterm /usr/bin/top -n1 -b');
echo "<pre>$var</pre>";
?>
Upvotes: 1
Reputation: 59571
Your script looks OK, let's make sure that your environment is correctly set up by changing the script to do something very simple. Try replacing all code with something like
<?php
echo "Hello World";
If that works, then some further debugging: Are you on a shared webhost where PHP is possibly configured to disable execution of scripts? See if shell_exec
is disabled as detailed here (replace the string exec
with shell_exec
).
Upvotes: 0
Reputation: 452
Since the user browser is not an interactive terminal then we have to execute your last command in every postback, in order to simlate the linux top
command.
Upvotes: 0