php - 记录PHP脚本的实时运行时间

标签 php email loops cron

假设我的 set_time_limit 设置为 30 秒且无法更改。我有一个 php 电子邮件脚本,它将在 1 个 cron 作业上运行超过 30 秒(我还没有达到该时间标记。) 30 秒后超时会发生什么?脚本会停止然后继续吗?

如果没有,我想记录从脚本循环开始到达到 30 秒的耗时,并暂停该过程然后继续。

有什么好的方法可以做到这一点?

更新:我认为可能有效的方法

function email()
{
  sleep(2); //delays script 2 seconds (time to break script on reset)

  foreach ($emails as $email): 
       // send individual emails with different content
       // takes longer than 30 seconds
   enforeach;

   // on 28 seconds
   return email(); //restarts process
}

最佳答案

建议的方法:

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();
foreach ($emails as $email): 
   // send individual emails with different content
   // takes longer than 30 seconds

   $time_curr = microtime_float();
   $time = $time_curr - $time_start;
   if($time > 28){ //if time is bigger than 28 seconds (2 seconds less - just in case)
        break;// we will continue processing the rest of the emails - next time cron will call the script
   }
enforeach;

关于php - 记录PHP脚本的实时运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620989/

相关文章:

javascript - javascript 中 json 的循环问题

python - 如何使用2个循环,如果为假,则无限期地重复该程序

php - Apache 权限被拒绝

php - 只有一排回响

php - CakePHP 空字段与非空字段验证

java - 从 PHP 脚本返回到 java 程序的字符串无法正常工作

php - 多边形中的geoPHP点

java - 为什么使用 apache-email 库时附件文件名损坏?

node.js - 在Node.js中使用MongoDB中的电子邮件模板

javascript - 编写一个代表 8×8 网格的程序?