0

I have a long running script that can run for awhile. (It sends an email every 5 seconds) to many users. This script is triggered via an ajax request

  1. If the response is never received such as the browser is closed, will the script continue to run? (It appears it does, but are there any conditions on when it won't?

  2. Does sleep count towards the max execution time. (It appears this is false also)

Chris Muench
  • 17,444
  • 70
  • 209
  • 362
  • You probably should be using cron or a job queue. – datasage Mar 06 '13 at 20:41
  • agreed, I was just wondering. I am using apache 2 and php 5 – Chris Muench Mar 06 '13 at 21:00
  • Not really what you asked but might be useful to you. http://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php You could run such a script from the cli asynchronously allowing your ajax client to disconnect with no effect on your email process. – Gavin Mar 06 '13 at 21:39

2 Answers2

1

1. The short answer is: it depends. In fact, it can be configured in PHP and in web server you use. It depends on the mode you use PHP in (module or CGI or whatever). You can configure it sometimes, though. There is an option in php.ini:

/* If enabled, the request will be allowed to complete even if the user aborts
; the request. Consider enabling it if executing long requests, which may end up
; being interrupted by the user or a browser timing out. PHP's default behavior
; is to disable this feature.
; http://php.net/ignore-user-abort
;ignore_user_abort = On*/

2. Almost always sleep does count. There are conditions when it does not, but in that case not the execution time is measured but execution cpu time. IIS does count CPU usage by app pools, not sure how it applies to PHP scripts.
It is true that PHP does not kill a script that is in sleep right now. That mean that the script will be killed once the sleep is over (easy test: add sleep(15); to your php and set max execution time to 10. You will get an error about time limit but in 15 seconds, not in 10).
In general, you can not rely on freely using sleeps in script.

You should not rely on script that is run without a user (browser) within a web server, either. You are apparently solving a problem with wrong methods: you really should consider using cron jobs/separate tasks.

Aneri
  • 1,342
  • 8
  • 21
0
  1. This depends on the server. Some servers could terminate the script when the socket is closed (which will probably happen when the browser is closed), others could let the script execute until the timeout is reached.

  2. Again, would depend on the server. I can really see a implementation looking at the time the script puts load on a CPU, then again - just measuring how long ago the script was started is an equally good approach. It all depends on what the person(s) making the server software was after.

If you want definite answers I would suggest sifting through the documentation for the webserver and php-implementation your script is running on.

fredrik
  • 6,483
  • 3
  • 35
  • 45