php function re-executed after about 15 minutes - php

I have a php script which is runtime should take about 20-25 minutes, and approximately 15 minutes during execution time it simply re-executing for no apparent reason.
I gave it a token to make sure it kills the second execution before it runs again, but it's not the solution I prefer to use in the long run.
Some details about the script:
It ignites after a socket connection (socket does not timeout nor restarting at any point of the session.)
I gave the php script ignore_user_abort(true) in order to prevent the script from ending when user goes somewhere else.
I don't get any execution runtime errors, or any errors at all... In the client side I get a GET error message to the script after 15 minutes, which is actually the second execution try. It's still running and socket is still delivering data, though it appears the script has ended.
There are no two separated requests
I'm not sure what could be the problem of it, hopefully one of the masters here could hint with a clue?
Much thanks!

Related

PHP script runs much longer than max_execution_time?

PHP is running as an Apache module.
The script start with a: ini_set('max_execution_time', 300);
What it does is basically connecting to a database, doing a big SELECTquery and looping through the results, writing them to a file and echoing a "result ok" after each write with explicit flush();
There is no sleep() call.
This is a "test" script made from a co-worker of mine for backup purposes and is intended to run up to a few hours! I thought I was aware of script execution time limit and thought his script would time out after 300 seconds...
But it didn't !
It's invoked from a web browser. The page is left open and we can see the results being echoed in real-time.
Why doesn't it time out?
Even stranger, one of the test issued a "Maximum execution time of 300 seconds exceeded" but this appeared at least after 2 hours of execution!
What's going on here? Is there something to understand between max_execution_time and flush() or a browser window being kept opened?
As you can see on the man page for the set_time_limit function, here the total execution time you are setting only affects the actual script. The time spent on database queries or any other external calls is not counted (if the OS is not Windows).
The only thing I can think of that may cause this is if PHP is running in safe mode. ini_get('max_execution_time') should tell you if it's actually being set.
Edit: Spotted your comment above.
echo ini_get('max_execution_time'); // says 300
If 300 is reported, and your not on Windows, #mishu is likely right. Your SELECT query is probably taking hours.

long running php script called via ajax (Don't want to wait for response)

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
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?
Does sleep count towards the max execution time. (It appears this is false also)
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.
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.
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.

PHP script being interrupted at random time

I have a php script that does a lot of stuff, produces a lot of output and can take a few minutes to complete.
I have already placed a set_time_limit(999999) at the beginning.
Before that, it obviously exceeded the time limit of 30 seconds and got interrupted. When that happened, a fatal error was logged indicating just that.
Now, from the browser I call that script, I observe as the browser receive the output from the script almost in real time, but at a random point, the script just is interrupted. It does not exceed the time limit (obviously), and NO ERROR at all is logged. Actually, if I try it a second time it gets interrupted at a different point.
The question is, if no error is encountered, and the time limit is not reached, what else can cause the script to be interrupted? Perhaps a disconnection from the client (browser) due to timeout? (not timeout waiting for data, though, as data is constantly being output). Perhaps the browser may have a maximum size for an html page?
But if it's one of these two cases, how comes this is not logged as an error?
When the script reaches the time limit, a fatal error is thrown, so if you are not seeing any error, this shouldn't be the cause (Make sur that error reporting is activated, either set it in the script or in the php.ini file).
If you want to desactivate time limit use this instead :
set_time_limit(0);
Without more informations, we can't be of much help unfortunately.

PHP set_time_limit no effect

I have a very painful slow script that gets lots of data from MySQL and creates a large report out of it that it serves to the user at the end as application/force-download.
Long story short, on production server it keeps terminating after about 30 seconds (quite consistently) and spitting out an empty file instead. On development server it works fine, but it does take significantly longer to execute - about 90 seconds. Just to be 'safe', I set my php.ini file to max_execution_time = 2000 and also run set_time_limit(4000) in the beginning of my script (numbers way over the expected completion time, but just to be sure ;)).
What could be causing my web server to ignore the time limits I set and quit on me after only 30 seconds?
Edit: one thing I know for sure is that it takes MySQL portion of the code 8-9 seconds to complete, and it successfully gets past that point every time.
Maybe the PHP safe_mode.
Try to do a
die(ini_get('max_execution_time'))
to read the value after you have called the set_time_limit(0); to see if actually it get overwrited.
If it gets overwrited to 0 and your script still dies then the cause could be somewhere else in your code

PHP script seems to crash during sleep command

I've got a php script that queries an external API that allows a limited amount of queries in a time period. To handle this I sleep my script for 60 seconds if I get a message back that I've hit this limit and then wake up and check again then repeat this until I can get more data back from the API.
The problem seems to be that sometimes during that sleep (somewhat rarely) the script will either crash, or never return. I'm not certain which yet all I can tell is that the script it doesn't pick back up and restart processing.
I'm looking for any tips or ideas on what could be happening so I have an idea of what to look for to fix this.
Thanks.
Can you post some of the code?
Since you are not sure if the script is crashing or never returning, it is possible that there is another area of your script that isn't hitting the sleep command, and just finishes executing.
check the max execution time in php.ini
$time=ini_get('max_execution_time');
$slept=0;
while(!dataready()){
sleep(1);
$slept++;
if($slept>=$time-4) die("script timed out");
}

Categories