Link to home
Start Free TrialLog in
Avatar of dirknibleck
dirknibleck

asked on

How to run a process so that it won't timeout

I have a scheduling program in a web application written in PHP and MySQL, that times out every time it is run without fail. It does it's job, it just doesn't get to the end before the server times out, so my users need to keep launching it until all of the schedule slots are filled. I do not believe that I can make my process much more efficient than it currently is, so I need to figure out a way for the server to keep running it, even though it may take a long time (ie. 5 - 10 minutes)

My thought is that it must be possible to give the process over to the server so that it is not associated with the user's session. I suspect that if I were to run the process via Cron, it would run to completion without timing out.

What are my options here? My app is hosted on a regular, everyday LAMP provider where I only have as much control as offered by the cPanel.
Avatar of cyberstalker
cyberstalker

Just add

set_time_limit(1200);

In the beginning of the script.
Avatar of dirknibleck

ASKER

I haven't had success with set_time_limit in this script. It still times out after about 5 minutes. Is there no way to make a process run like a batch job? What if I want the process to keep running even if a user were to exit their browser?
ASKER CERTIFIED SOLUTION
Avatar of cyberstalker
cyberstalker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What does the flush() do?

For sending data to the browser could I send back % complete and have an AJAX listener, or will that only run once http stops returning?
By default, PHP buffers output. This means that your output will only actually get send when the script finishes, when the output buffer is full or when you implicitly flush() the buffers.

So if you don't do the flush(), nothing will get send to the browser at all and the browser will still stop listening.

Sending a completion percentage to an AJAX listener will not do you much good, unless you apply a http push technique. I prefer to use multipart/x-mixed-replace documents, which are supported by most browsers, except Internet Explorer. You can also use a framework for it, like Comet.