Php check if shell_exec command was successful Php check if shell_exec command was successful shell shell

Php check if shell_exec command was successful


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}


You should use exec instead of shell_exec if you need only the exit code of the command.


You can do this:

$output = shell_exec('ls -lart');echo "<pre>$output</pre>";

and validate the output.

But according to the manual:

Note: This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

http://php.net/shell_exec