简体   繁体   English

如何在特定时间间隔内运行PHP脚本(例如每天一次)?

[英]How can I run PHP script in certain interval (e.g. once a day)?

I have a php script that reads one file through http(the file is on other domain). 我有一个PHP脚本,通过http读取一个文件(该文件在其他域上)。 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? 除了使用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. 我不想使用cron因为我更喜欢在脚本本身设置这种行为..因此它很灵活,所以我可以在任何地方使用它而无需每次都设置cron。 thanks 谢谢

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());
    }
}

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. 如果您不能或不想使用cron,只有在访问页面时才可以更新它。 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. 您可以缓存HTTP请求的结果,只有在缓存超过一天或您选择的任何时间间隔时才在页面上加载它。

如果要以定时间隔访问站点,也可以使用基于Web的Cron

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. 然后,在运行脚本之前,只需检查每天的行数。

You can use a Cronjob . 你可以使用Cronjob You can then run the php script by the command line. 然后,您可以通过命令行运行php脚本。

php /someplace/somefile.php

The Cronjob would be the following if you update every day. 如果您每天更新,Cronjob将是以下内容。

0  0  *  0  0  php /someplace/somefile.php

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. 由于您明确声明不想使用cron,因此执行此操作的唯一方法(没有类似于cron的方法)是将脚本设置为守护程序。 However, unless you really need the flexibility that daemons provide, cron is much easier and simpler. 但是,除非你真的需要守护进程提供的灵活性,否则cron更容易和更简单。

Here's one daemon walk-through . 这是一个守护进程

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 . 我需要安排基于年度的工作,独立于应用程序(如果系统重新启动或类似的事情),我得到了使用systemd计时器的建议。 The Arch Wiki Page on it gives some examples. Arch Wiki页面上有一些例子。

如果您使用的是* nix环境,则可以使用cron作业

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". 一般来说,PHP在Web服务器的上下文中运行有一个时间限制它可以执行多长时间,所以通常你不能设置“后台”PHP线程来“稍后”做东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何每月在服务器上运行php脚本而不运行cron作业。 例如使用.htaccess - How can i run a php script on a server every month without running a cron job. e.g. using .htaccess 如何请求在输入元素中一次输入某个数字(例如0)? - How could I request a certain digit (e.g. 0) to be entered once in an input element? 如何提取某些HTML标签,例如 <ul> 在PHP中使用带有preg_match_all的Regex? - How can I extract certain HTML tags e.g. <ul> using Regex with preg_match_all in PHP? 如何对 SQL 查询进行排序,但将某些 UTF-8 字符排序为正常等效字符? (例如É被视为E等) - How can I sort an SQL query but have certain UTF-8 characters be ordered as their normal equivalent? (e.g. É be regarded as E etc) 如何使用PHP计算目录中的所有文件? (例如124个文件) - How can I count all files in a directory with PHP? (e.g. 124 Files) 如何在PHP中使用Ajax从网站(例如IMDB)中获取数据 - How can I fetch data from a website (e.g. IMDB ) using Ajax in PHP 如何在PHP中循环$ num,例如1、2、3、4、1、2、3、4、1、2、3、4 - How to loop $num in php, e.g. 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 如何每天使用PHP代码查找特定的时间间隔 - How can I find a certain time interval in every day using PHP code 我如何在jQuery中的页面加载时播放动画,以便它播放index.php而不播放other_page.php - How can I play an animation on page load in jQuery so that it plays for index.php but not e.g. other_page.php PHP - 如何在某一点将“...”附加到不同长度的字符串? 例如,200个字符之后 - PHP - How to append “…” to a string of varying length at a certain point? e.g. After 200 chars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM