Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php check if shell_exec command was successful

I basically want to check if a command ran successfully 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

like image 681
utdev Avatar asked Sep 30 '16 12:09

utdev


People also ask

How do you know if an exec is successful?

<? php if (exec('C://abc//wkhtmltopdf home. html sample. pdf')) echo "PDF Created Successfully"; else echo "PDF not created"; ?>

How to use shell_ exec in php?

The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix. If the command fails return NULL and the values are not reliable for error checking.

How do you check if exec is enabled in PHP?

php phpinfo(); ?> You can search for disable_functions and if exec is listed it means it is disabled. To enable it just remove the exec from the line and then you need to restart Apache and you will be good to go. If exec is not listed in the disable_functions line it means that it is enabled.

What is Shell exec?

Updated: 05/04/2019 by Computer Hope. On Unix-like operating systems, exec is a builtin command of the Bash shell. It lets you execute a command that completely replaces the current process. The current shell process is destroyed, and entirely replaced by the command you specify.


1 Answers

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
}
like image 149
Mister M Avatar answered Sep 30 '22 16:09

Mister M