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 );
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.
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.
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.
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.
Try to put chmod 0777 to the executable file:
chmod($file,0777);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With