如何实现php脚本的单处理模式运行?
我有 cron job - php 脚本,5 分钟内调用一次。我需要确保之前调用的 php 脚本已完成执行 - 不想混合正在处理的数据。
我使用了三种方法:
创建包含运行状态标志的辅助文本文件。执行的脚本分析文件的内容,如果标志设置为 true,则中断。这是最简单的解决方案,但每次我创建这样的脚本时,我都觉得我又发明了一辆自行车。是否有任何众所周知的模式或最佳实践可以满足大多数需求?
添加 UNIX 服务。这种方法最适合 cron 作业。但开发和测试 UNIX 服务更耗时:需要良好的 bash 脚本知识。
使用数据库跟踪进程。很好的解决方案,但有时不鼓励再次使用数据库 - 不想发明自行车,希望已经有一个很好的灵活解决方案。
也许您对如何管理 php 脚本的单一处理有其他建议?很高兴听到您对此的想法。
I have cron job - php script which is called one time in 5 minutes. I need to be sure that previously called php script has finished execution - do not want to mix data that's being processed.
There are three approaches I used to apply:
Creation of auxiliary text file which contains running-state flag. Executed script analyzes the contents of the file and breaks if flag is set to true. It's the simplest solution, but every time I create such script, I feel that I invented a bike one more time. Is there any well-known patterns or best-practices which would satisfy most of the needs?
Adding UNIX service. This approach is the best for the cron jobs. But it's more time consuming to develop and test UNIX service: good bash scripting knowledge is required.
Tracking processes using database. Good solution, but sometimes database usage is not encouraged and again - do not want to invent a bike, hope there is a good flexible solution already.
Maybe you have other suggestions how to manage single-processing of php scripts? Would be glad to hear your thoughts about this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用文件锁定机制。您创建一个文本文件,然后让您的进程以独占方式锁定它(请参阅 php集群函数: https://www.phpflock.com/flock )。 php.net/flock)。如果它无法锁定,那么您将退出,因为还有另一个实例正在运行。
使用文件锁定的优点是,如果您的 PHP 脚本意外终止或被终止,它将自动释放锁定。如果您使用纯文本文件作为状态,则不会发生这种情况(如果脚本设置为在执行结束时更新此文件并且意外终止,您将留下不真实的数据)。
I'd recommend using the file locking mechanism. You create a text file, and you make your process lock it exclusively (see php flock function: https://www.php.net/flock). If it fails to lock, then you exit because there is another instance running.
The advantage of using file locking is that if your PHP scripts dies unexpectedly or gets killed, it will automatically release the lock. This will not happen if you use plain text files for the status (if the script is set to update this file at the end of execution and it terminates unexpectedly, you will be left with untrue data).
http://php.net/flock 与 LOCK_EX 对于您的情况应该足够了。
http://php.net/flock with LOCK_EX should be enough in your case.
您可以在 grep 命令的帮助下使用 ps 命令检查脚本当前是否正在运行。如果您需要有关这些命令的信息,“man ps”和“man grep”将告诉您有关这些 unix/linux 命令的所有信息。
假设您的脚本名为“my_script.php”。这个 unix 命令:
...将告诉您脚本是否正在运行。您可以在脚本开头使用 shell_exec() 运行此命令,并 exit() 如果它已经在运行。
此方法的主要优点是它不会出错,否则脚本可能会崩溃,使您的标志文件处于一种让您认为它仍在运行的状态。
You could check wether or not your script is currently running using the ps command, helped by the grep command. "man ps" and "man grep" will tell you all about these unix/linux commands if you need informations about these.
Let's assume your script is called 'my_script.php'. This unix command :
...will tell you if your script is running. You can run this command with shell_exec() at the start of your script, and exit() if it's already running.
The main advantage of this method is that it can't be wrong, where the script could have crashed, leaving your flag file in a state that would let you think it's still running.
我会坚持使用版本号 1。它很简单并且有效。只要您只想检查脚本是否已完成,它就足够了。如果要记住更复杂的数据,我会选择版本 3,以便能够“记住”相关数据...
hth
K
I'd stick to version number 1. It's simple and works out. As long as you only wan't to check whether the script has finished or not it should be sufficent. If more complex data is to be remembered I'd go for version 3 in order to be able to 'memorize' the relevant data...
hth
K