tags:

views:

203

answers:

6

For example , there is a very simple php script which updates some tables on database, but this process takes long time (maybe 10 minutes). Therefore , i want this script to continue to this process even if user closed the browser, because sometimes users do not wait and close the browser or go to another webpage .

A: 

A server-side script will go on what it is doing regardless of what the client is doing.

EDIT: By the way, are you sure that you want to have pages that take 10 minutes to open? I suggest you to employ a task queue (whose items are executed by cron on a timely basis) and redirect user to a "ok, I am on it" page.

shanyu
... not totally true: usually, there are guards to "kill" long-running php scripts: these take the form of crontab entries.
jldupont
Not always true: http://php.net/manual/en/function.ignore-user-abort.php
timdev
@jldupont: I am no php guy, but does these scripts have anything to do with the client?
shanyu
@shanyu - not sure what jldupont is talking about. There are configuration variables for php that can kill long-running scripts, but they have absolutely nothing to do with cron.
timdev
+1  A: 

if the script is completely server based (no feedback to the user) this will be done even if the client is closed.

The general architecture of PHP is that a clients send a request to a script that gives a reply to the user. if nothing is given back to the user the script will still execute even if the user is not on the other side anymore. More simpler: their is no constant connection between server and client on a regular script.

yopefonic
Note always true: http://php.net/manual/en/function.ignore-user-abort.php
timdev
A: 

You can make the PHP script run every 20 minutes using a crontab file which contains the time and what command to run in this case it would be the php script.

Annerajb
that's besides the question asked here...
jldupont
+4  A: 

To answer your question directly, see ignore_user_abort

More broadly, you probably have an architecture problem here.

If many users can initiate this stuff, you'll want the web application to add jobs to some kind of queue, and have a set number of background processes that chew through all the work.

timdev
A: 

Yes. The server doesn't know if the user closed the browser. At least it doesn't notice that immediately.

No: the server probably (depending of how it is configured) won't allow for a php script to run for 10 minutes. On a cheap shared hosting I wouldn't rely on a script running for longer than a reasonable response time.

anonymous
+7  A: 

If the task takes 10 minutes, do not use a browser to execute it directly. You have lots of other options:

  • Use a cronjob to execute the task periodically.
  • Have the browser request insert a new row into a database table so that a regular cronjob can process the new row and execute the PHP script with the appropriate arguments
  • Have the browser request write a message to queue system, which has a subscriber listening for such events (which then executes the script).

While some of these suggestions are probably overkill for your situation, the key, combining feature is to de-couple the browser request from the execution of the job, so that it can be completed asynchronously.

If you need the browser window updated with progress, you will need to use a periodically-executed AJAX request to retrieve the job status.

DavidWinterbottom
+1 for a much nicer, and concrete, version of my answer.
timdev
So if i want to de-couple the browser request from the execution of the job , how can i request another php script ,while user's current request continues normaly . So , when user request any url , this request will launch another script (which works independently from users browser and other requests). And user will continue to surf on the site normallyHow can i do this ?
Oguz
See those bullet points? Those are all ways to do this. In most cases, setting up a little database table that holds inputs for the background process works fine. Then you can kick off some PHP script to execute the jobs, either via cron, or some other method.
timdev
it should be started by user , because it has variables which are given by user . Thats why i can not use cron job . Secondly process is not about maintaining database , it is about downloading file , this is why i can not use queue system .. these are the reasons why i gave another structure , but i do not know how to create that .
Oguz
use your brain. You stick a job record on the queue in the database. That record can contain any data the user gave you. The cron job looks at the queue, grabs the next job, reads the arguments the user gave, and then uses those inputs to do its job. Read David's second bullet point carefully. It's exactly what you want to do.
timdev
how can i execute the php URL (not script) which was recorded to a new row in database , because i use framework. so regular cron job can process
Oguz