doushu0591 2018-11-28 08:02
浏览 295
已采纳

PHP - sleep()在经过一段时间后无法继续执行

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.

  • 写回答

1条回答 默认 最新

  • dougan1465 2018-11-28 08:07
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?