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.
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 );
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:
Apparently, the PHP exec() function calls the Linux exec() system call. If your executable is a bash script, you have to start it explicitly with /bin/bash:
exec( "/bin/bash /usr/local/bin/myexecutablefile /full/path/to/someotherfile.js", $output, $retval );
since you cannot use an exec() system call on Linux to run a shell script; only a shell like bash can interpret the commands that are in there. /bin/bash is an executable that can be perfectly started with exec() on Linux, which in turn runs your script.