Search code examples
phpsleep

PHP - sleep() cannot continue execute after time has passed


currently I implement the sleep() in my PHP script to pause the execution for 15 minutes to give some buffer for other tasks to finish before continue to execute the script.

<?php

------do something here-----

   file_put_contents("logs/pre_sleep.log", "$id sleep in $datetime", FILE_APPEND);

   sleep(900);    

----continue do something here----

   file_put_contents("logs/done.log", "$id done in $datetime", FILE_APPEND);

?>

When the PHP is called from client, the process will be logged in "pre_sleep.log" and before the script finish will be logged in "done.log". However, based on the log I write, I notice the some execution will not continue even has passed for 15 minutes. The process has been logged in "pre_sleep.log" where "done.log" couldn't find the same process.

Is it possible the process been killed by others within 15 minutes? This happens seem like very randomly, because most of the processes logged in both log files but some appear in only one.


Solution

  • It is possible the process gets shut down after the sleep (or even before the sleep).

    My best guess is, your PHP timeout is in play. Put the following at the top of your script file.

    set_time_limit(0);
    // or
    ini_set('max_execution_time', 0);
    

    It will allow for scripts to run forever