Php:Capturing the Command Output

php : Capturing the command output

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

Php shell command output on to a variable

Use shell_exec:

$var=shell_exec('ls');

or exec:

exec('ls', $output, $return_var);

# print array
foreach($output as $content){
echo $content . "\n";
}
print "return_var:" . $return_var . "\n";

How to catch SSH command output contain sub string ? - Laravel 5.8

Because SSH::run uses a closure, $resultFromServer does not exist within the scope of the sub function. You need to pass it in with the use keyword as well as pass it in as a reference since you're modifying it on the inside:

SSH::run($command, function($line) use (&$resultFromServer)
{
echo $line.PHP_EOL;

if($line!= null) {
array_push($resultFromServer, $line); // crash
}
});

PHP Use proc_open and capture stdout of 'echo' command

You're reading from $pipes[0]. This is stdin. You want to read from $pipes[1] which is mapped to stdout (and has the w flag for being written to):

$descriptors = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']];
$handle = proc_open('echo Hello world, $USER!', $descriptors, $pipes, null, ['USER' => 'guest']);
$world = stream_get_contents($pipes[1]);
var_dump($world);

Outputs

string(20) "Hello world, guest!
"

Capture/supress all output from php exec including stderr

Command proc_exec() allows to deal with file descriptors of the exec-ed command, using pipes.

The function is: proc_open ( string $cmd , array $descriptorspec , array &$pipes […optional parameters] ) : resource

You give you command in $cmd (as in exec) and you give an array describing the filedescriptors to "install" for the command. This array is indexed by filedescriptor number (0=stdin, 1=stdout…) and contains the type (file, pipe) and the mode (r/w…) plus the filename for file type.

You then get in $pipes an array of filedescriptors, which can be used to read or write (depending of what requested).

You should not forget to close these descriptors after usage.

Please refer to PHP manual page (and in particular the examples): https://php.net/manual/en/function.proc-open.php

Note that read/write is related to the spawned command, not the PHP script.

Capturing and displaying exec/shell_exec periodic output?

You don't see any output to output.txt because it's being buffered. For python there's an option to make it line-buffered. From the manpage:

       -u     Force  the  binary I/O layers of stdin, stdout and stderr to be unbuffered.  The text I/O layer will still be
line-buffered.

So your command would then become:

python -u myscript.py > output.txt

For PHP the flush function should help you.

capture imagemagick command output in php

exec() only places the output to STDOUT in the output array, however various imagemagick tools also output messages to STDERR. You can redirect messages from STDERR to STDOUT (and hence also get them in $output array), by appending this at the end of your command: 2>&1

Alternatively if you want to be able to differentiate where the messages were originally output, you can use proc_open that allows you to specify separate pipes for STDOUT and STDERR, and capture output from them separately.



Related Topics



Leave a reply



Submit