Capture / suppress all output from php exec, including stderr

I want to run several commands with exec(), but I do not want to display. However, I want to hold onto the output so that I can control verbosity as my script works.

Here is my class:

<?php
class System
{
    public function exec($command, array &$output = [])
    {
        $returnVar = null;
        exec($command, $output, $returnVar);
        return $returnVar;
    }
}

The problem is that most applications put an annoying amount of irrelevant material in stderr, which I seem to be unable to block. For example, here is the output from execution git clonethrough it:

Cloning into '/tmp/directory'...
remote: Counting objects: 649, done.
remote: Compressing objects: 100% (119/119), done.
remote: Total 649 (delta 64), reused 0 (delta 0), pack-reused 506
Receiving objects: 100% (649/649), 136.33 KiB | 0 bytes/s, done.
Resolving deltas: 100% (288/288), done.
Checking connectivity... done.

I saw that other questions claim that using the output buffer may work, however it does not work

<?php
class System
{
    public function exec($command, array &$output = [])
    {
        $returnVar = null;
        ob_start();
        exec($command, $output, $returnVar);
        ob_end_clean();
        return $returnVar;
    }
}

. stderr stdout , stdout stderr, Windows Linux, .

<?php
class System
{
    public function exec($command, array &$output = [])
    {
        $returnVar = null;

        // Is Windows
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            exec($command, $output, $returnVar);
            return $returnVar;
        }

        // Is not windows
        exec("({$command}) 2>&1", $output, $returnVar);
        return $returnVar;
    }
}

stderr, stdout ?

/

@hexasoft , , :

<?php
class System
{
    public function exec($command, &$stdOutput = '', &$stdError = '')
    {
        $process = proc_open(
            $command,
            [
                0 => ['pipe', 'r'],
                1 => ['pipe', 'w'],
                2 => ['pipe', 'w'],
            ],
            $pipes
        );

        if (!is_resource($process)) {
            throw new \RuntimeException('Could not create a valid process');
        }

        // This will prevent to program from continuing until the processes is complete
        // Note: exitcode is created on the final loop here
        $status = proc_get_status($process);
        while($status['running']) {
            $status = proc_get_status($process);
        }

        $stdOutput = stream_get_contents($pipes[1]);
        $stdError  = stream_get_contents($pipes[2]);

        proc_close($process);

        return $status['exitcode'];
    }
}

, .

+4
1

proc_exec() exec-ed, .

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

$ cmd ( exec), , "" . (0 = , 1 = …) (, ) (r/w…), .

$ pipe , ( , ).

.

, PHP (, , ): https://php.net/manual/en/function.proc-open.php

, / , PHP.

+2
source

Source: https://habr.com/ru/post/1611950/


All Articles