如何在没有 cron 帮助的情况下用 PHP 制作调度程序
如何在不编写 cron 脚本的情况下用 PHP 制作调度程序?有标准溶液吗?
功能[例如]:订阅到期后 24 小时向所有订阅者发送剩余内容。
How can I make a scheduler in PHP without writing a cron script? Is there any standard solution?
Feature [For example]: sent remainder to all subscriber 24hrs b4 the subscription expires.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
标准解决方案是在类 Unix 操作系统上使用 cron ,在 Windows 上使用计划任务。
如果您不想使用
cron
,我想您可以尝试使用at
来配置一些东西。但很难想象cron
是个问题但at
没问题的情况。The standard solution is to use
cron
on Unix-like operating systems and Scheduled Tasks on Windows.If you don't want to use
cron
, I suppose you could try to rig something up usingat
. But it is difficult to imagine a situation wherecron
is a problem butat
is A-OK.我看到的解决方案是循环(for 或 while)和
sleep(3600*24);
The solution I see is a loop (for or while) and a
sleep(3600*24);
通过 javascript 每隔设定的时间间隔发送 ajax 调用来执行它
Execute it through a sending ajax call every set interval of yours through javascript
在急于实施之前,请先阅读我在底部的最终意见。
Cron 确实是安排事情的最佳方式。它简单、有效且广泛可用。
话虽如此,如果 cron 不可用或者您绝对不想使用它,那么在传统 Web 服务器上运行非 cron、Apache/PHP 伪 cron 的两种通用方法如下。
使用可加载资源进行检查
在每个网页的某处嵌入图像/脚本/样式表/其他。图像可能是浏览器最支持的(如果关闭 javascript,则无法保证浏览器甚至会加载 .js 源文件)。此页面会将标头和空数据发送回浏览器(1x1 清晰的 .gif 就可以 - 查看 fpassthru)
来自 php 手册注释
检查每个页面加载
对于您想要自动化的每个任务,您将创建某种可调用的 API - 静态 OOP、函数调用 - 等等。对于每个请求,您都会检查给定任务是否有任何工作要做。这与上面类似,只是您不为脚本使用单独的 URL。这可能意味着在执行工作时页面需要很长时间才能加载。
这将涉及在记录上次运行任务的任务表上对数据库进行选择查询,或者直接在有问题的数据上(在您的示例中,可能在订阅表上)进行选择查询。
最终意见
如果可能的话,你真的不应该重新发明轮子。 Cron 非常容易设置。
然而,即使您认为 cron不容易设置起来,请考虑这一点:对于站点上的每个页面加载,您将产生检查以查看的开销需要做什么。另一方面,真正的 cron 会按照您设置的时间表(每小时等)执行命令行 PHP,这意味着您的服务器运行任务检查代码的频率要低得多。
没有真正 cron 的最大潜在问题
您面临着网站没有足够流量来实际足够频繁地进行更新的风险。
Please read my final opinion at the bottom before rushing to implement.
Cron really is the best way to schedule things. It's simple, effective and widely available.
Having said that, if cron is not available or you absolutely don't want to use it, two general approaches for a non-cron, Apache/PHP pseudo cron running on a traditional web server, is as follows.
Check using a loadable resource
Embed an image/script/stylesheet/other somewhere on each web page. Images are probably the best supported by browsers (if javascript is turned off there's no guarantee that the browser will even load .js source files). This page will send headers and empty data back to the browser (a 1x1 clear .gif is fine - look at fpassthru)
from the php manual notes
Check on each page load
For each task you want to automate, you would create some sort of callable API - static OOP, function calls - whatever. On each request you check to see if there is any work to do for a given task. This is similar to the above except you don't use a separate URL for the script. This could mean that the page takes a long time to load while the work is being performed.
This would involve a select query to your database on either a task table that records the last time a task has run, or simply directly on the data in question, in your example, perhaps on a subscription table.
Final opinion
You really shouldn't reinvent the wheel on this if possible. Cron is very easy to set up.
However, even if you decide that, in your opinion, cron is not easy to set up, consider this: for each and every page load on your site, you will be incurring the overhead of checking to see what needs to be done. True cron, on the other hand, will execute command line PHP on the schedule you set up (hourly, etc) which means your server is running the task checking code much less frequently.
Biggest potential problem without true cron
You run the risk of not having enough traffic to your site to actually get updates happening frequently enough.
创建一个cronjob表。其中保留 cron 作业的日期。如果今天的日期等于 creonjob 表中的日期,则保留一个条件。然后调用一个方法来执行。这就像 CRON 工作一样工作得很好。
Create a table of cronjob. In which keep the dates of cron job. Keep a condition, if today date is equal to the date in the creonjob table. then call for a method to execute. This works fine like CRON job.