Php system() / exec() don't return output - php

For common commands like 'ls', exec() works fine, eg:
exec('ls',$output,$retval);
var_dump($output,$retval);
// $output contains an array of filenames, and retval === 0
But when trying to execute another program, I can't get the output:
exec('some_command --a_parameter',$output,$retval);
var_dump($output,$retval);
// $output contains an empty array, end $retval === 0
This command prints some lines when executing it directly from the command-line though.
I know the command has been successful because of the results (some files updated, data added, etc), and yet I can't see the output.
Any ideas?

It sounds like the program is outputting its warnings to standard error rather than standard output. exec will only catch standard output. I don't know for certain that standard error is always sent to the apache error log, but it seems likely.
If you don't need compatibility with non-*nix systems, you can redirect standard error to standard output by appending 2>&1 to the command:
exec('some_command --option 2>&1', $output, $ret);
This should both make the warnings available to your php program and prevent unnecessary logging.

All the output was being sent to httpd/error_log when for some * reason, when the application found a warning (something internal, not even fatal)
My solution: When there is no output, assume it failed.
My apache logs are going to get dirty, but whatever.

Related

Processing Gem Data

I have a question regarding running a shell command via PHP. My goal is to successfully run compass compile [project] via PHP. I have tried the following:
echo system('compass compile [project]', $s); // prints [31m[0m
echo $s; // prints 1
echo passthru('compass compile [project]', $p); // prints [31m[0m
echo $p; // prints 1
echo shell_exec('compass compile [project]'); // prints [31m[0m
echo exec('compass compile [project]', $e, $ee);
print_r($e); // Array ( [0] => [31m[0m )
echo $ee; // prints 1
I even tried running a shell command to an executable file that contained compass compile test and I still got the same results as the trials above.
My questions
What does [31m[0m mean? Does this represent binary data? Do these represent bash colors as search engines suggest?
As far as I know, the output should be the following:
For kicks, I tried to execute via system(/usr/local/bin/compass compile [project]); and I got the same result. I double checked my path so I know I can execute these commands as expected. Here is the output from echo $PATH:
/usr/lib/lightdm/lightdm:
/usr/local/sbin:
/usr/local/bin:
/usr/sbin:
/usr/bin:
/sbin:/bin:
/usr/games:
/usr/local/games:
/var/lib/gems/1.9.1/bin
Is there a way to compile compass projects using PHP?
I've seen a similar error before.
Typically it is due to the things being output in the bash startup scripts. For example, I had an echo in one of my bash startups that jacked up a lot of scripts till I realized what was causing the problem.
Or, perhaps the user (www-data ?) doesn't actually have a home dir and appropriate startup scripts in place?
You can try this to get a non interactive shell:
exec("/bin/bash -c \"compass compile [project]\"", $e, $ee);
print_r($e);
echo $ee;
If you still have issues, try redirecting the output to a tmp file, an checking it:
exec("/bin/bash -c \"compass compile [project] > /tmp/compass.compile.output\"", $e, $ee);
print_r($e);
echo $ee;
See also: What's the difference between .bashrc, .bash_profile, and .environment?
The issue was fixed by using sass --compass and redirecting the stderr to stdout via echo shell_exec("sass --compass [project] 2>&1");
It was a pretty long and arduous process figuring this out since it's been awhile since I've dabbled in command line programs. Remember that error streams and output streams might be on different outputs. The easiest way to test this is to shovel the output into a file via:
# do this once with a good file and once with a file that will give errors
sass --poll style.scss > output.txt
If output.txt is empty then the error output is on the stderr stream such as the case above. We can correct this by redirecting the stderr to the srdout. For example:
sass --poll > output.txt 2>&1
#shows results
cat output.txt
I created a simple PHP script that redirects the output from one textarea to the other. The code can be found here.
First guess would be a permissions issue. Odds are the user account running PHP (unless you're running this from the command line, I'm guessing that this is the user that the httpd apache daemon is running under) doesn't have the permissions to do what you're asking. The errors are extremely unhelpful, however.
From what I can tell, it looks like an attempt to have the error show up in red on the command line. My guess is that there are hidden (or somehow never printed out) characters in-between the codes. Check out some of your apache and/or PHP error logs to see if anything helpful is showing up there that never made it into the PHP variable. Or, for kicks, try copy and pasting the output with the bash colors into a basic text editor and first delete each character from the beginning one by one... see if anything magically appears. If that doesn't work, try the same in reverse, backspacing from the end. Either way, there's an error occurring, so important that it must show in bold red letters to you, it's just not getting to you.
If it does in fact turn out to be a permissions issue, and it's one you can't remedy through permissions wrangling, you could create an intermediary file that your Apache user has permissions to write to, and your cron user has permissions to read from. Instead of running the code directly from PHP, put it in the file, then run a cron on a frequent basis looking for anything in that file, CHECKING IT FOR VALIDITY, and then running it through the compiler and removing it from the file.
It'd be ugly, but sometimes pretty things don't work.
You guessed it right it is colors but the way it is defined is not right. For more information regarding using colors in console please refer to this document. Also, for compiling SCSS via compass you can use shell_exec command in linux. For more information regarding shell_exec please refer to this document. Let us know how it goes.

PHP on Windows using system function - Why errors not in the correct place

I am using the PHP system function on windows as follows:
system("mysql --user=root --password=xxxx --host=127.0.0.1 --verbose <create.sql > out.txt 2>&1", $retVal);
But the error messages apppear at the top of the file. Is it possible to get the error messages to appear after the appropriate line of SQL that has the error.
The problem is that stdout is buffered and stderr is not meaning that the errors will always show first in this case. You are going to need to wrap your system call in a script and sort it afterwards if you wish to retain some sort of order. There is some explanation available over here.

php : Capturing the command output

I am using exec function to execute the specific executable files in php .
exec ( $file , $output , $return_value ) ;
When the given file executed successfully I am able to get the output in the second argument
by checking the return values , So, It is working fine. But My requirement is when the command execution getting failed due to some reason. I need to get that error message of that executed program . What I need to do to get the error . through second argument we can get the successful output only . Not error message.
Thanks.
The second argument $output only captures STDOUT from your executable. Error messages are usually sent to STDERR so that they easily can be written to an error log or similar, but this means that you won't see them when you call exec.
If this is a linux system, you could append 2>&1 to your command, in order to redirect STDERR to STDOUT. I haven't tried this, but it should forward the error messages to your $output variable.
Edit:
I've read up on it on www.php.net/exec, and it seems this would work.
exec($file.' 2>&1', $outputAndErrors, $return_value);
It is also possible to redirect the errors to a temporary file and read them separately.
exec($file.' 2> '.$tmpFile, $outputOnly, $return_value);
Edit 2
It seems windows also uses this Bourne style output redirecting syntax, so the examples should work for windows too.
More on input and output streams
If you need to keep stderr and stdout separate, try proc_open:
http://php.net/manual/en/function.proc-open.php
The $return_value will have the error code returned by the program which should be meaningfull enough, I don't think you can have better.

Running lame from php

I am trying to run lame from a php script.
I have tried these, but no luck, I don't get anything returned! Any ideas?
system('lame', $returnarr);
system('lame --help', $returnarr);
exec('lame', $returnarr);
passthru('lame', $returnarr);
even this one returns nothing:
exec('which lame', $returnarr);
I am on OSX and final deployment will be on Linux. Do you have better suggestions for an automated wav->mp3 conversion?
From php, should I execute a bash script that executes Lame?
Try something like this:
$output = array();
$result = -1;
exec('`/usr/bin/which lame` --help 2>&1', $output, $result);
var_dump($output, $result);
$output should be an array of lines contained in the output
$result should be an integer result code. 0 is typically success, >=1 is an error (specific codes are application dependant).
The 2>&1 part will redirect STDERR to STDOUT ($output) which would normally be dropped. So if it's erroring out, you should be able to see the error (hopefully).
If you get -1 for the dump of $result, there's a fundimental problem, because that's not a valid result code (it likely means that exec is disabled, or the process you're trying to run is restricted because of permissions errors or the such)...
If you feel a need for more convenient way to work with lame, I would recommend to use phplame wrapper. Install PHP LAME wrapper using Composer:
{
"require": {
"b-b3rn4rd/phplame": "dev-master"
}
}
set error reporting on and check if you can do exec's. By default most systems wont allow it, it's a serious security liability. You've got to explicitly allow execs in php.ini.
Might be a $PATH problem. Try specifying the full path to lame, ie. /usr/local/bin/lame.

PHP #exec is failing silently

This is driving me crazy. I'm trying to execute a command line statement on a windows box for my PHP web app. It's running on windows XP, IIS5.1. The web app is running fine, but I cannot get #exec() to work with a specific contactenated variable. My command construction looks like this:
$cmd = ($config->svn." cat ".$this->repConfig->svnParams().quote($path).' -r '.$rev.' > '.quote($filename));
This command does not work as is above, when it generates the following string:
svn --non-interactive --config-dir /tmp cat "file:///c:/temp/test/acccount/dbo_sproctest.sql" -r 1 > "C:\Inetpub\sites\websvn\temp\wsv5B45.tmp"
If I copy/paste this to my own command line, it works fine.
If I hard code that very same path instead of adding it with the variable, it works! I've tried with and without quotes around the file name. I've tried with and without quotes around the entire command. I've tried other directories. I've tried passing an output paramter to exec(), and it comes back empty (Array () ). I've tried redirecting the output of the error stream of the command to a file, and that error output file never gets created.
The only thing I can possibly concieve of is that exec() is failing silently. What on earth am I doing wrong here? If I hard code the file path, using the same dir structure and filename, it works fine. If I don't, it doesn't.
Maybe the slashes () in the file path aren't being escaped properly, but when I do it manually with single quotes they are not considered escape sequences??
UPDATE:
I took the # off of exec, and still not seeing any errors.
I gave the full path to SVN, still no luck. It should be noted that the command worked fine before with the non-full path SVN so long as I manually specify the file destination for cat.
Update 2: RE: Kieth
I'm calling exec by trying both:
exec($cmd);
or
exec($cmd, $out);
My php.ini already had safe_mode = 0.
I added error_reporting(E_ALL); and didn't see anything new
If I echo (or print_r) my exec call, I am not actually seing anything
If I echo (or print_r) my exec call when included an output var, I get an empty arr
Update 3
I tried both escapeshellcmd and escapeshellarg to no avail (good idea though).
I should add that the file is being created through invoking
tempnam("temp", "wbsn");
The fact that it works just fine if I manually specify the string instead of letting it be generated by tempname seems to suggests that the source of the problem, but I can't figure out how. I did a comparison of the manual string with the one generated, and it came back as a match.
#exec will always fail silently, because # is PHP's error suppression operator.
Not sure if this will help you since you're on Windows and I'm on Linux, but I ran into this same problem of silent errors from PHP exec(). I figured out that the command I was attempting to issue (nconvert) sends its error messages to the standard error stream, not standard out. So I added
2>&1
at the end of the command line to redirect my error back to the standard stream. Then I could see that nconvert was giving me a permission denied error.
It could be that the PATH isn't the same from your php script vs your user account. Try removing the # and see if it's trying to throw an error.
In addition, you may want to try putting the full filesystem path to the SVN executable.
Well, if removing # isn't working, try including this at the start of the piece of code you're running:
error_reporting(E_ALL);
That'll turn on full error reporting, just in case you have it turned down or disabled in your php.ini file.
I don't suppose you could show us how exactly you're calling exec()? Could you also check to make sure you're not accidentally running the script in safe mode? Are you echoing out what exec() is returning, and if so, what is it returning?
I'm not a big fan of adding another response, but if I just edit my previous response, you may not see it.
PHP has some special escaping commands for shell scripts: escapeshellcmd and escapeshellarg.
I think you should be able to use escapeshellcmd around your entire $cmd, but I'm not sure.
Have you tried echo exec("dir") or something simple to see if exec() is working at all?
I don't know what going on, but I at least have a workaround.
This works:
$tmp = tempnam("./", "wbsn");
$filename = dirname($tmp).'\\temp\\'.basename($tmp);
This, however, does not, but I would have expected it to generate the same path (diff file name since it's a new tempnam()).
$tmp = tempnam("temp", "wbsn");
Also, this does not work, which I also would expect to generate the same thing:
$tmp = tempnam("temp", "wbsn");
$filename = dirname($tmp).'\\'.basename($tmp);
All 3 of these solutions appear to generate the same file paths, but only the first one actually works when used in my exec. I have no clue why it does not.
Visual inspection (echo) of all 3 of these appear to generate the same paths (with the exception of filenames differing, of course). A string comparison of dirname() of each of these 3 shows as a match. I have no clue what the deal is, but the first one is a workaround.
A very useful trick when debugging shell exec problems is to place an "echo" at the beginning of the command. Make sure that you can view the standard output somewhere.
This will let you examine the command for any obvious problems. Perhaps it has a wildcard that is expanding unexpectedly, or perhaps the shell quoting is not exactly right. The echo will let you see this, and you can cut and paste the echoed command into another shell to see if it is working properly.
My advice will be to switch from WIN, IIS to linux, but as alternative you can try this:
function exec_alt($cmd) {
exec($cmd, $output);
if (!$output) {
/**
* FIXME: for some reason exec() returns empty output array #mine,'s machine.
* Somehow proc_open() approach (below) works, but doesn't work at
* test machines - same empty output with both pipes and temporary
* files (not we bypass shell wrapper). So use it as a fallback.
*/
$output = array();
$handle = proc_open($cmd, array(1 => array('pipe', 'w')), $pipes, null, null, array('bypass_shell' => true));
if (is_resource($handle)) {
$output = explode("\n", stream_get_contents($pipes[1]));
fclose($pipes[1]);
proc_close($handle);
}
}
return $output; }

Categories