如何以一定的时间间隔(例如每天一次)运行 PHP 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/865381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run PHP script in certain interval (e.g. once a day)?
提问by Adriana
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
我有一个 php 脚本,它通过 http 读取一个文件(该文件在其他域上)。我想每天只读一两次这个文件,而不是每次刷新网站时都连接到它。 除了使用 cron 之外,还有其他方法吗?我不想使用 cron 因为我更喜欢在脚本本身中设置这种行为..所以它很灵活,所以我可以在任何地方使用它而无需每次都设置 cron。谢谢
采纳答案by Alo
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.
如果您不能或不想使用 cron,则可以仅在访问页面时更新它。您可以缓存 HTTP 请求的结果,并且仅在缓存超过一天或您选择的任何时间间隔时才在加载它的页面上更新它。
回答by cOle2
I've done this kind of thing in the past when I didn't have access to cron:
过去当我无法访问 cron 时,我做过这种事情:
$lastRunLog = '/path/to/lastrun.log';
if (file_exists($lastRunLog)) {
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 86400) {
//its been more than a day so run our external file
$cron = file_get_contents('http://example.com/external/file.php');
//update lastrun.log with current time
file_put_contents($lastRunLog, time());
}
}
回答by Sheraz
You can also use Web Based Cronif you want to hit a site on a timed interval.
如果您想按时间间隔访问某个站点,也可以使用 基于 Web 的 Cron。
回答by Strae
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.
您甚至可以使用数据库表——结构、ID、日期、脚本 url 和任何你需要的东西都很简单——并在每次运行脚本时添加一行。
Then, before run the script simply check the numbers of row for each day you have.
然后,在运行脚本之前,只需检查您每天的行数。
回答by Eldila
回答by scotts
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.
由于您明确声明您不想使用 cron,因此唯一的其他方法(没有类似于 cron 的东西)是将您的脚本设置为守护进程。但是,除非您真的需要守护进程提供的灵活性,否则 cron 会更容易和更简单。
Here's one daemon walk-through.
这是一个守护进程演练。
回答by Abdul
If you're using a Linux distro with systemd:
如果您使用带有 systemd 的 Linux 发行版:
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 Pageon it gives some examples.
我需要安排每年一次的工作,独立于应用程序(以防系统重新启动或类似情况),有人建议我使用systemd Timers。其上的 Arch Wiki 页面提供了一些示例。
回答by nsayer
What's wrong with cron?
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.
您有几个使用 cron 的选择 - 您的 php 可以由命令行 PHP 解释器调用,或者您可以使用 wget 或 fetch 或等效物在服务器上调用您的 PHP。
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".
通常,在 Web 服务器上下文中运行的 PHP 对其可以执行多长时间有时间限制,因此通常您不能设置“后台”PHP 线程来“稍后”执行操作。

