PHP Exec: Does Not Return Output

PHP: Why isn't exec() returning output?

You should redirect stderr to stdout.

To do that, change your exec() call like this:

exec("ping -c 1 $domain_bad 2>&1", $output, $return_var);

More info about 2>&1 meaning here.

PHP exec output does not return and execute commands

Thanks for contributing. The problem was that i did not set > msg.log 2> msg2.log.

The line WARNING: Requested formats are incompatible for merge and will be merged into mkv. was run in 2> and therefore stopped the execution.

The command --no-warnings might have solved it too.

PHP system(), exec(), and shell_exec() not returning output

I think that the issue is not that you not get the output of the executed command, but which fails to find mysql.
Using exec you can get the return status of your command, where 0 means successful, and other values indicate an error.

$output = exec($cmd, $output, $retval);
var_dump($output);
var_dump($retval);

If the $retval is 1 that would mean which doesn't find the mysql binary, and returns an empty line.

php exec: does not return output

There are a few posts to the relevant sections of the PHP Manual such as this one:

I was having trouble using the PHP exec command to execute any batch
file. Executing other commands (i.e., "dir") works fine). But if I
executed a batch file, I receieved no output from the exec command.

The server setup I have consists of Windows Server 2003 server running
IIS6 and PHP 5.2.3. On this server, I have:

  1. Granted execute permissions to the Internet User on c:\windows\system32\cmd.exe.
  2. Granted Everyone->Full Control to the directory in which the batch file is written.
  3. Granted Everyone->Full Control on the entire c:\cygwin\bin directory and its contents.
  4. Granted the Internet User "log on as batch" permissions.
  5. Specified the full path to each file being executed.
  6. Tested these scripts running from the command line on the server and they work just fine.
  7. Ensured that %systemroot%\system32 is in the system path.

It turns out that even with all of the above in place on the server, I
had to specify the full path to cmd.exe in the exec call.

When I used the call: $output = exec("c:\\windows\\system32\\cmd.exe
/c $batchFileToRun");

then everything worked fine. In my situation, $batchFileToRun was the
actual system path to the batch file (i.e., the result of a call to
realpath()).

There are a few more on both the exec and shell_exec manual pages. Perhaps following through them will get it up and working for you.

Get and return result from php exec

yourfile.php

    <?php
$value = "a";
exec("php -f test.php $value", $output);
print_r($output);
$array = json_decode($output[0], true);
print($array["foo"]);

$value = "b";
exec("php -f test.php $value", $output2);
print_r($output2);
?>

test.php

<?php
if($argv[1] == "a"){
$array = array(
"foo" => "bar",
"bar" => "foo",
);
echo json_encode($array);
} else {
echo "it wasn't an a";
}
?>

To obtaint "true" or "false" if the exec succeded :

exec("php -f test.php $value", $output, $return); /* print($return) */

$output contain everything displayed by the php script, that's why you should use json to display your object and then encode it back in his state.



Related Topics



Leave a reply



Submit