php - 如何检查php脚本是否已经在运行

标签 php cron while-loop

如何创建不可停止的 while 循环, 如果它已经在运行,请不要再次运行它,我假设如果脚本运行它将更新数据库中的最后一个 cron 时间,并且脚本将检查当前时间 - 40 秒是否小于最后一个 cron 时间。我所拥有的样本,但这是重叠的(一次又一次地运行 do process)谢谢!

<?php
ignore_user_abort(1); // run script in background 
set_time_limit(0);    // set limit to 0


function processing()
{
//some function here
setting::write('cron_last_time', $time); // write cron last time to database
}

$time = current_timestamp();  // current time
$time_before = $time - 20;   //

$settings = setting::read('module_cron_email'); // Get last cron time from database ( in array )
$time_last = $settings['setting_value'];     // Last cron time get value from array



if ($time_before < $time_last) // check if time before is less than last cron time
{ 
   do{
   processing(); //run our function
   sleep(7);    // pause for 7 seconds
   continue;   // continue
   }
   while(true); // loop
}else{

echo "already running";
die();
}
?> 

最佳答案

启动脚本时,检查锁定文件。如果确实存在,则退出或跳过循环。如果不存在,则创建文件并让它运行。

关于php - 如何检查php脚本是否已经在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10463992/

相关文章:

php - Memcached "Could not connect"高峰时段错误

php - 两个用户可以共享相同的浏览器和站点但具有不同的 session 吗

ruby-on-rails - Cron 作业在 Whenever gem 中不起作用

swift - 为什么 year 返回值为 1,而它应该随着每个循环而增加?

Javascript - 检测何时加载图像

php - 在php脚本中声明多个变量以节省空间

php - Mysql Real Escape String PHP函数添加 "\"到我的字段条目

java - JVM 崩溃,没有任何崩溃转储,崩溃报告被 apport 忽略

mysql - 带日期的 Cron 文件名正在传递 Y_m_d 而不是实际日期

python - While 循环监视文件夹并在条件为真时运行脚本