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:
- Granted execute permissions to the Internet User on c:\windows\system32\cmd.exe.
- Granted Everyone->Full Control to the directory in which the batch file is written.
- Granted Everyone->Full Control on the entire c:\cygwin\bin directory and its contents.
- Granted the Internet User "log on as batch" permissions.
- Specified the full path to each file being executed.
- Tested these scripts running from the command line on the server and they work just fine.
- 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
Symfony\Component\Httpkernel\Exception\Notfoundhttpexception Laravel
PHP MySQL_Real_Escape_String() -> Stripslashes() Leaving Multiple Slashes
Returning First X Items from Array
Measure String Size in Bytes in PHP
How Do Check If a PHP Session Is Empty
Check If Laravel Model Got Saved or Query Got Executed
Reusing the Same Curl Handle. Big Performance Increase
PHP Float Calculation Error When Subtracting
How to Detect If Have to Apply Utf-8 Decode or Encode on a String
PHP Remove Elements from Associative Array
Removing Black Borders 4:3 on Youtube Thumbnails
Multiple Forms and One Processing Page
I'm Getting This Error Code: Invalid Argument Supplied for Foreach()
Secure and Flexible Cross-Domain Sessions
Downloading a Folder Through with Ftp Using PHP
Targeting Specific Email with the Email Id in Woocommerce
Swift_Transportexception Connection Could Not Be Established with Host Smtp.Gmail.Com