A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearWhy the php code below is not returning the expected values? - page 1
User InfoPosts
Why the php code below is not returning the expected values?#1
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.

posted date: 2009-04-14 08:59:00


Re: Why the php code below is not returning the expected values?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-14 08:59:01


Re: Why the php code below is not returning the expected values?#3
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?

posted date: 2009-04-14 09:06:00


Re: Why the php code below is not returning the expected values?#4
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?

posted date: 2009-04-14 09:11:00


Re: Why the php code below is not returning the expected values?#5
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.

posted date: 2009-04-14 09:22:00


Re: Why the php code below is not returning the expected values?#6
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.<?phpexec((/usr/bin/top -n 1 -b(,$output);echo (<pre>(,implode("\n",$output),(</pre>(;

posted date: 2009-04-14 11:59:00


Re: Why the php code below is not returning the expected values?#7
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>";?>

posted date: 2009-04-14 12:08:00


Re: Why the php code below is not returning the expected values?#8
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

posted date: 2009-04-14 12:19:00


Re: Why the php code below is not returning the expected values?#9
Or you could just curl out to it

posted date: 2009-04-14 12:20:00


Re: Why the php code below is not returning the expected values?#10
Ole J. Helgesen, Eddy both your answers worked fine. I just don't want to choose one answer vs the other. I will wait for some votes to come it to choose the right answer. Thanks to both of you.

posted date: 2009-04-14 13:04:00


Re: Why the php code below is not returning the expected values?#11
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

posted date: 2009-04-14 15:41:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.