PHP and exec()


#1

For some reason I can’t seem to get exec() to execute php scripts in the background.

For example I have a file called test.php that reads contains the line:

exec("/usr/local/php5/bin/php proc.php >/dev/null &");

When I run this php script from the shell ($:php test.php) it works fine and calls proc.php but if I call this same script from the web (http://www.somesite.com/test.php) it dosn’t work right. It executes PHP but when I have the output sent to a log file (>log.txt instead of /dev/null) it says php found no input file!

Can anyone help me out with this? I have PHP-CGI running, its not an issue of exec being denied permission. It works calling other programs with arguments (ie: exec(“ping -c 10 www.google.com >log.txt &”) will run ping in the background and send its output to log.txt when called via HTTP).

Thanks in advance!


#2

exec() is disabled in apache’s mod_php for security reasons. Your only option is to compile and install your own PHP.

-Scott


#3

Its not disabled though in php-cgi which is the default and what this enviroment is… I said I can call it and it runs things like ping or even shell scripts fine, but for some reason it wont run php scripts, it will start php but for some reason php wont see the script passed to it. It wont even run php scripts called inside shell scripts… but it will run the shell script itself… its very odd.


#4

Very odd, it worked back in February. So the only way to get ImageMagick to work is to install your own version of php?
Silk


#5

Oops, I’m a bit backwards, new setups don’t even use mod_php, so that’s not it. I wonder if it’s an issue of PHP4 vs PHP5. Is your site set for PHP4?

-Scott


#6

It’s ok :stuck_out_tongue: I’m running 5 on there, I’ll try with 4 but I don’t think thats the issue, as I have run it via command line using 4 and 5 and it works under both on command line…


#7

Uh lol well I changed it to PHP4, tried to run it again via http and it spawned like hundreds of processes… now I can’t ssh in… lol really odd seeing that it didnt do that under PHP5… or maybe it was a fluke. Who knows…

Argh…


#8

I guess we learned something new today. I don’t know what, though. Good luck getting it sorted out. It’ll be interesting to hear what the problem is.

-Scott


#9

Yea… it wasnt a fluke, PHP4 breaks exec() on gobstopper at least when you call it via CGI… I contacted tech and they either caught it before it crashed the server again or it figured itself out… but uh yea, I don’t think I will be trying that with PHP4 again!


#10

OK PHP seems to be the issue, as I can call perl scripts via exec() in PHP… but not PHP scripts via http…


#11

Man I feel like an idiot. User error. I had two copies of my script and forgot about it. So exec is working with ver 4 and cgi.
Silk


#12

Anyone have any idea why it would return 404 not found via exec() when you call it via the website, but not when you call it from the command line???


#13

sorry to bring up an old issue, but has anyone found a solution to this?
i’m running php 5.2.5 as fast cgi and am having this same problem
not being able to execute a php script using exec() from the web, and it working perfectly fine from the command line

another thing i noticed, is that the exec-ed script spawns several times with different pids

dave


#14

coolies, i found the answer, should’ve been using the cli php binary, and not the cgi binary