每 5 分钟运行一次 PHP 脚本并避免竞争条件

2023-08-18php开发问题
3

本文介绍了每 5 分钟运行一次 PHP 脚本并避免竞争条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个需要每 5 分钟运行一次的 php 脚本.目前我正在使用 cron 作业来运行它(并且效果很好),但我的主机只允许至少 15 分钟的时间.

I have a php script that needs to run once every 5 minutes. Currently I'm using a cron job to run it (and it works great) but my host only allows a minimum time of 15 minutes.

所以我的问题是,我可以使用访问者每 5 分钟触发一次 php 脚本的运行吗?我可以轻松地记录上次运行的时间,然后根据经过的时间重新运行.

So my question is, can I use visitors to trigger the running of a php script every 5 minutes. I can easily just record the last time it ran, and re-run it based on elapsed time.

但是,我担心竞争条件.重要的是脚本每 5 分钟只运行一次.

However, I'm worried about race conditions. It is important that the script only gets run once every 5 minutes.

我的脚本需要大约 60 秒才能运行.在此期间它会写入几个文件.如果脚本运行不止一次,它会损坏文件.另外,如果我在 10 分钟内没有访问者,那么在下一个访问者到达时运行一次就可以了.

My script takes about 60 seconds to run. It writes to a couple files during this time. If the script ran more than once it would corrupt files. Also, if I get no vistors for 10 minutes, then running once when the next vistor arrives is fine.

是否有一些标准方法可以完成此任务?

Is there some standard way to accomplish this task?

谢谢!

推荐答案

您是否考虑过让您的脚本运行无限循环并带有 sleep 以在迭代之间等待 5 分钟?

Have you considered just having your script run an infinite loop with a sleep to wait 5 minutes between iterations?

for (;;)
{
  perform_actions();
  sleep(300);
}

或者,您可以拥有一个文件(例如,is_running),并获得一个独占的 在脚本开始时锁定,并在最后释放.至少这样你就不会做任何破坏性的事情.

Alternatively, you could have a file (for example, is_running), and get an exclusive lock on it at the start of your script which is released at the end. At least this way you will not do anything destructive.

您也可以将这两种解决方案结合起来.

You could also combine these two solutions.

$fp = fopen("is_running", "r+");

/* is it already running? */
if (! flock($fp, LOCK_EX | LOCK_NB)) return;

for (;;)
{
  perform_actions();
  sleep(300);
}

然后让 cron 作业每 15 分钟运行一次.如果进程仍在运行,它就会退出,否则它会重新启动并每 5 分钟恢复更新一次.

And then have the cron job still run every 15 minutes. If the process is still running, it will just bail out, otherwise it will relaunch and resume updating every 5 minutes.

这篇关于每 5 分钟运行一次 PHP 脚本并避免竞争条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17