如何以一定的时间间隔(例如每天一次)运行PHP脚本?
我有一个 php 脚本,它通过 http 读取一个文件(该文件位于其他域上)。 我只想每天阅读该文件一两次,而不是每次刷新网站时都连接到它。 除了使用 cron 之外还有其他方法吗? 我不想使用 cron 因为我更喜欢在脚本本身中设置此行为..所以它很灵活,所以我可以在任何地方使用它而无需每次都设置 cron。 谢谢
I have a php script that reads one file through http(the file is on other domain). I would like to read this file only once or twice a day, instead of connecting to it every time the website is refreshed.
Is there any other way than doing it with cron?
I dont want to use cron cause I prefer to setup this behaviour in the script itself .. so it is flexible, so I can use it anywhere without setting up cron every time.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当我无法访问 cron 时,我过去做过这种事情:
I've done this kind of thing in the past when I didn't have access to cron:
如果您不能或不想使用 cron,并且仅在访问页面时更新它就可以了。 您可以缓存 HTTP 请求的结果,并且仅在缓存超过一天或您选择的任何时间间隔时才在页面加载时更新它。
If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.
您甚至可以使用数据库表 - 结构非常简单,包含 id、日期、脚本 url 以及您需要的任何内容 - 并在每次运行脚本时添加一行。
然后,在运行脚本之前,只需检查每天的行数。
You could even use a database table - really simple in structure, id, date, script url, and whatever you need - and add a row every time you run the script.
Then, before run the script simply check the numbers of row for each day you have.
您可以使用Cronjob。 然后您可以通过命令行运行 php 脚本。
如果您每天更新,Cronjob 将如下所示。
You can use a Cronjob. You can then run the php script by the command line.
The Cronjob would be the following if you update every day.
由于您明确声明不想使用 cron,因此执行此操作的唯一其他方法(没有类似于 cron 的内容)是将脚本设置为守护程序。 然而,除非您确实需要守护进程提供的灵活性,否则 cron 会更容易、更简单。
这是一个守护进程演练。
Since you explicitly state that you don't want to use cron, the only other way to do this (without something analogous to cron) is to set up your script as a daemon. However, unless you really need the flexibility that daemons provide, cron is much easier and simpler.
Here's one daemon walk-through.
如果您使用带有 systemd 的 Linux 发行版:
我需要安排每年的作业,独立于应用程序(以防系统重新启动或类似的情况),并且建议我使用 systemd 定时器。 Arch Wiki 页面 提供了一些示例。
If you're using a Linux distro with systemd:
I had a need for scheduling yearly based jobs, independent of the application (in case the system rebooted or anything like that), and I was given the suggestion to use systemd Timers. The Arch Wiki Page on it gives some examples.
如果您在 *nix 环境中,您可以使用 cron 作业
If you are on a *nix environment you can use cron jobs
克朗有什么问题?
对于 cron,您有多种选择 - 您的 php 可以由命令行 PHP 解释器调用,或者您可以使用 wget 或 fetch 或等效方法在服务器上调用 PHP。
一般来说,在 Web 服务器上下文中运行的 PHP 对其执行时间有时间限制,因此一般情况下您无法设置“后台”PHP 线程来“稍后”执行操作。
What's wrong with cron?
You have a couple choices with cron - your php can be invoked by the command line PHP interpreter, or you could use wget or fetch or the equivalent to invoke your PHP on the server.
In general, PHP run from within the context of the web server has a time limit on how long it can execute, so in general you can't set up "background" PHP threads to do stuff "later".