I need to call a php file from my browser without it timing out(as it normally will when I call it) Is there a way I can do this that will prompt the file to run in the background and allow the page I call it from to continue loading without waiting for that script to finish up.... Basically prompting an immediate job call, but something that I dont really need to see the results of for an hour or so. Thanks, Rob
No, that wont work ... The webserver is the problem, not the php... So, I want to call a php script(running in the background) when I visit index.php. The page visited by me, as the user would be index.php, and it would be to start the processing of the php script which typically lasts 10 minutes or so. During this time, I the user, could be visiting other pages instead of waiting for that script to finish running(or in most cases, the webserver times out)
If I read you post correctly 10 minutes for a php script to run is rediculous. You need to make your page more efficient, it is most likely using a lot of resources as a result.
It is parsing different pages, as well as processing those pages. I say 10 minutes, but when it is running for 10 users at once, doing lots of things for each user, then it takes longer. But either way, I am looking for a solution to the problem, not advise about script optimization.
haha .. ok, these arguments can go on all day... Parsing documents from other websites can take a long time, period.... The point is, I understand that it will take a while to process, I understand the resources required, but I still want to be able to call it from the web, and let it process in the background.... Now with those things in mind, do you know of a solution. If not its ok, you dont have to comment on the lack of optimization.
Even if I did know of a solution I wouldn't offer it with your foul attitude towards people that are willing to help.
I don't know about fixing the timeout problem... However a staged approach might be appropriate. Cron 1 calls the setup, initialises the list of pages to retrieve and gives a status of "pending" Cron 2 then runs every x minutes and calls any pages with a status of "pending". Evenutally it'll be doing nothing.
That could work ... I would prefer to have something that starts immediately upon, say, hitting a form button... which would then take me to the next page(step) in the users process... and then by the time they are done with the different steps, the script has finished running and will show the data on the main page when the user returns to it later. I would think that there would be some sort of solution for this as it would allow a user to not experience the long processing that I require, but still be able to reap the benefits of it ASAP. Thanks for the proposed solution, Sarah... I think that would definately work as a plan B if there is nothing that would complete the task exactly as I desire it. Can I call exec() to do anything like this possibly??
exec might, but you'd have to have the server rights to use it. You may be able to use phpFork http://www.phpclasses.org/browse/file/4017.html or if you have php5 use the fork function www.php.net/fork
As was said earlier...you can use exec if that function has not been disabled. Let's say you have a script called 'longRunning.php' and this is the one that needs to run in the background. If you simply call exec() with the parent script it will hang waiting on it to complete. Thus, this does not solve your issue. In order for you to do this successfully you need to do two things: (1) redirect output somewhere other than stout (release the streams) and (2) actually send the process to the background. Here is an example: exec('/usr/bin/php ./longRunning.php > /dev/null 2>&1 &'); PHP: Enjoy! Bobby
Awesome ... I do have exec privileges... Can you break down that exec() call and tell me what each part of the call is doing?? Thanks for the help so far!!
Yep, you could use shell_exec() or exec() to execute system commands. This will allow you to run the script in the background. You could also have cron configured run the script automatically. Though note that having exec, shell_exec() and certain other functions is very risky so you better make sure your script is bullet proof. If it isn't, you can always enable it with ini_set().