PHP - 好的 cronjob/crontab/cron 教程或书籍
我正在寻找一个好的 cronjob 教程或书籍来学习如何使用 PHP 创建一个.
I'm looking for a good cronjob tutorial or book to learn how to create one using PHP.
推荐答案
Cronjob 不是创建为 PHP 进程或脚本的东西.Cron 是一个 Linux 程序,它允许您定期调用脚本.
Cronjob is not something to create as Php process or script. Cron is a linux program that allows you to call a script at a regular interval.
您可以通过以管理员用户身份进入您的 linux 机器并键入:
You can see what is an crontab by entering in your linux machine as an admin user and type:
root@valugi:~# crontab -e
你会看到类似的东西
*/1 * * * * /usr/bin/php /var/www/somesite/public/cron.php
这意味着我每分钟都在执行 cron.php.
This means that each minute I am executing the cron.php.
现在,您可能希望在不同时间执行不同的脚本,并希望将此逻辑传递到 php 级别而不是 linux 级别.如果是这种情况,您可能希望以最低的时间分母(例如分钟)调用您的 cron 脚本,并在 cron.php 中构建一些将在不同时间调用其他脚本的逻辑.
Now, you may want to have different scripts executed at different times and want to pass this logic to php level instead of linux level. If this is the case you may want to call your cron script at the lowest time denominator (minute for example) and in the cron.php build some logic that will call at different times other scripts.
我使用例如 Cronable 接口:
I use for example a Cronable interface:
interface Cronable {
public function cron();
}
并且每个想要被cron.php调用的类都必须实现这个接口和cron()函数,它会指定具体的调用频率是多少.cron.php 将获取所有这些类,并将当前时间与该时间进行比较,并决定是否执行调用.
And each class that wants to be called by the cron.php has to implement this interface and the cron() function, which will specify what is the specific frequency of the call. The cron.php will get all this classes and will compare current time with that time and will decide to execute the call or not.
相关文章