Php check if shell_exec command was successful

utdev picture utdev · Sep 30, 2016 · Viewed 8.7k times · Source

I basically want to check if a command ran successful using shell_exec.

Simple function:

public static function foo()
{
    $command = "blabla";
    shell_exec($command);
}

Edit, I tried Mister M's suggestion like this:

    foreach($commands as $key => $value)
    {
        shell_exec($value, $output, $return);
    }

And I get this error:

Undefined variable: output

Answer

Mister M picture Mister M · Sep 30, 2016

Try using exec:

$output = array();//Each line will be assigned to this array if any are generated.
$result1 = exec($command, $output, $return);

if ($return != 0)
{
 // error occurred
}
else
{
 // success
}