Can't execute PHP script using PHP exec

Solution 1:

I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:

exec("php-cli  somescript.php");

This worked for me.

Solution 2:

What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.

if you just want the script's output, try:

exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')

however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:

exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')

the above should only output the "Hello World" to the execoutput.

Edit:

Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:

alias pexec='php /home/quote2bi/tmp/helloworld.php'

then

exec('pexec > /tmp/execoutput.txt 2>&1 &')

it seems to be a problem with the way exec handles input as opposed to the shell itself.

-John