Is there any problem if i use <?php sleep(60); ?>
in my script for 30 times, so that the script will load and finish its works in 30 minutes, is there chance of any problem?
Note: if it fails , is there any way to do like this?

- 533
- 7
- 18
-
3This is likely to fail. Your browser will time out after [between 30 and 120 seconds](http://stackoverflow.com/a/1343963/168868). – Charles Dec 09 '12 at 10:08
-
1Are you running this in the browser or in a command line? – str Dec 09 '12 at 10:12
3 Answers
The client is likely to stop listening for a response, but it is still possible for the script to complete execution. You would have to configure your PHP installation for long execution times though.
If you are using a browser and AJAX requesting data that will be returned after 30 minutes, it is better to poll a static resource.
When you want to start the script, send an AJAX request to it. This request will eventually time out. Set up your script so that it saves its output to a static resource when it is done (text file / image / etc; depends on what your script is supposed to return). Poll this resource every five minutes using AJAX, and you will get the response from the server when it is ready.

- 46,193
- 6
- 90
- 139
It makes no difference if you call sleep(60)
for 30 times or sleep(60 * 30)
once.
Take a look at the php function set_time_limit, because the standard is 30 seconds or so. After that, your script will be terminated.
However, I don't know if there is a browser willing to wait so long for your request. What are you trying to achieve?

- 5,291
- 2
- 29
- 33
There are several problems with this idea.
- sleep delays execution of the following code or the next loop for
n
seconds. This will be on top of your scripts "natural" run-time, so the total execution time will be( sleep(s) * times called ) + execution_time
when executing the script in a cgi environment or in the php apache module, by default you have a
max_execution_time
of 30s after which your script will stop execution. On CLI the default is 0 =~ unlimited. http://php.net/manual/en/info.configuration.php#ini.max-execution-timeFinally, as stated in the comments, if your script is called from a browser, the webserver will run into a timeout much earlier.

- 17,625
- 5
- 60
- 77