Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exec - PHP return_val is 126

I m trying to exec shell command within PHP script, but exec returns 126 code, which means "Command invoked cannot execute" (Permission problem or command is not an executable). But the funniest thing is that if I run the same php script under cli it works well. What's wrong with it?

Maybe there some issues with environment? Because when I run it under Apache it returns 127 code if I don't use absolute path to executable file (under cli it works well and return 0, even if I use just file name).The file is stored in /usr/local/bin folder.

UPDATE:

As asked in comment, I show example of my code, but there are nothing special.

This piece is working fine under cli, but doesn't work under apache ($retval will be equal to 127):

$output = array();
$retval = 0;
exec( "myexecutablefile /full/path/to/someotherfile.js", $output, $retval );
echo implode( PHP_EOL, $output );

This piece of code will return $retval = 126

$output = array();
$retval = 0;
exec( "/usr/local/bin/myexecutablefile /full/path/to/someotherfile.js", $output, $retval );
echo implode( PHP_EOL, $output );
like image 777
Eugene Manuilov Avatar asked Jul 16 '12 20:07

Eugene Manuilov


People also ask

What is PHP exec command?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly.

How do I know if PHP exec is enabled?

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.

Does PHP exec wait until finished?

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.


2 Answers

You need to specify the full path to the executable, as well as make sure your Apache user has rights to execute it.

Apache doesn't run with bash, and doesn't care about your own personal path setting.

like image 80
Brad Avatar answered Sep 22 '22 00:09

Brad


Try to put chmod 0777 to the executable file:

chmod($file,0777);
like image 45
montie Avatar answered Sep 19 '22 00:09

montie