views:

231

answers:

6

I am trying to make a few php scripts that will run among other things system calls commands like top and other custom scripts. You can see the code below is super simple. The problem that we are having with the php below is that when I call the php from the linux prompt like:

#php checkTOP.php

it will return the top -n 1 output in the screen no problem there.

When I run the script from the webserver using the http://url.com/checkTOP.php it only returns the following:

program:/usr/bin/top -n 1 ver1 = retval = 1 Returned not zero

Which is my debugging statements.

<?php
        $program="/usr/bin/top -n 1";
        echo "program:{$program}<br /> \n";
        $ver1=system($program,$retval);
        echo "ver1 = {$ver1}<br />\n";
        echo "retval = {$retval}<br /> \n";
        if($retval==0)
        {
                echo "Returned 0<br />\n";
        }
        else
        {
                echo "Returned not zero <br />\n";
        }
        die;
?>

Change 1: One more thing. All permissions are set correctly /usr/bin/top is set root:apache with rxrxrx and also all the directories /usr/bin.

+1  A: 

Sounds like it's a problem related to user accounts. When you run it from the command-line manually, you're probably running under a different user than the webserver tries to run as.

Some things to check:

  • is Safe Mode enabled for PHP?
  • does the user the webserver runs under (often "www-data") have permission to execute top?
  • can you turn on a higher error reporting level to see if you can get more information?
Chad Birch
Safe mode is off. I ran the php checkTOP.php with the following command sudo -u apache php checkTOP.php and it worked. I am looking into the reporting level. thanks.
Geo
+1  A: 

Depending on the information you want to gather from the system, it may be more useful, and certainly more secure to collect it from the /proc/ filesystem.

What are you trying to get from the command?

Alister Bulman
+1  A: 

One way to get top output is to send -b switch to run top in batch mode and use exec() to get output in an array with one element per line.

<?php
exec('/usr/bin/top -n 1 -b',$output);
echo '<pre>',implode("\n",$output),'</pre>';
Ole J. Helgesen
+2  A: 

Ole beat me; I used output buffering so I'll post it:

<?PHP
ob_start();
$program="/usr/bin/top -n 1 -b";
passthru($program);
$t=ob_get_contents();
ob_end_clean();
echo "<PRE>$t</PRE>"; ?>

Eddy
A: 

It looks like system function returns fail and retval is probably null in that case.

If you can't get the system command you could try using exec and see if it works. It will also give you back everything you want and I think it also returns an error message to help debug the issue:

http://us3.php.net/manual/en/function.exec.php

thirsty93
Or you could just curl out to it
thirsty93
A: 

The top command does not normally send its output to stdout. You cannot capture it with the system command. Use the -b option to top for "batch mode" and it should work for you.

$program="/usr/bin/top -b -n 1";

When you run it from the linux prompt, it looks like it's working because the output goes to the display. When I tried it, I noticed that the output did not quite match what I expected from the echo commands. That is what tipped me off that there was something strange about the output and a check of the man page for top confirmed the -b flag.

--
bmb

bmb