6
6
6a6ajka2022-04-18 18:44:56
PHP
6a6ajka, 2022-04-18 18:44:56

PHP script execution time limit on hosting, how to implement?

Roughly speaking, there is a similar script. I need it to run exactly 3 minutes.

while(true){

"тело скрипта"

sleep(10);
}


This script is hosted. We know from the PHP documentation:

max_execution_time
set_time_limit()

Does not include Sleep, System, http requests in the limit.

If I set limits on my script max_execution_time
300
set_time_limit(300)

the command has Sleep();

Server: nginx

But I can't change the ngnix settings on the

hosting

$.ajax({
    url: "https://url.con/ajax/test.php", // куда отправляем запрос
    error: function(){
        // будет вызвана в случае если пройдет 3 секунды, но ответа так и не было
      alert('no');
    },
    success: function(){
        // что-то делаем тут 
       alert('good');
    },
    timeout: 3000 // установка 3-х секундного тайм-аута
});


But when the timeout exits, the script continues to run.

What to do in this case?

the task will be run via Cron

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Shamanov, 2022-04-18
@6a6ajka

when run in console mode, there are a number of features, for example, by default, the execution time is not limited. check here for details
on each iteration
time() - $_SERVER['REQUEST_TIME'] >= max_execution_time() - 10

A
Alexander Karabanov, 2022-04-18
@karabanov

Limit the runtime of a cronjob or script

W
wadowad, 2022-04-18
@wadowad

Through such a timer, ajax will not be called every three minutes exactly. The timer over time will be more and more late (or will be stopped altogether if the tab is inactive). If the moment is critical with accuracy, you need to write the timer differently and disable "falling asleep" in the browser.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question