ImpressPages/Cron设置
来自站长百科
系统Cron
Cron是系统中定期运行的一个脚本,主要进行系统维护的工作。它可以检查系统的状态是否正常,可以删除一些旧的日志等等。
Cron脚本是怎样运行、什么时候运行:
默认情况下,该脚本每小时执行一次,使用“fake cron”,fake cron的意思是,当有人访问该网站时,该脚本将会被执行。当然,脚本不会在每一个页面访问下分别执行一次,在执行一次后的一个小时内它是不会再次被执行的。
如果你具有一些需要某一严格时间执行Cron jobs时钟作业的特殊插件的话,那么你需要通过cPanel面板或者是Linux命令行来设置时钟作业。
系统cron脚本文件的名称是ip_cron.php,我们可以通过访问地址 http://www.example.com/ip_cron.php来手动执行该脚本。
插件Cron
如果你安装的插件也需要定期执行维护任务的话,最好的添加这些代码的地方就是cron函数中。
创建步骤:
首先在你的插件目录下创建一个名字叫cron.php的文件,然后添加一个Cron类,如下所示:
<?php namespace Modules\group_name\module_name; //change module_group and module_name to actual values of your plugin. if (!defined('FRONTEND')&&!defined('BACKEND')) exit; //check if file is not accessed directly class Cron{ //leave intact /** * Is executed each time, when main system cron job is executed. * * Parameter $options store information about last executed cron job. * * $options->firstTimeThisYear - true if cron is executed first time this year * $options->firstTimeThisMonth - true if cron is executed first time this month * $options->firstTimeThisWeek - true if cron is executed first time this week * $options->firstTimeThisDay - true if cron is executed first time this day * $options->firstTimeThisHour - true if cron is executed first time this hour * $options->lastTimeBefore - last cron execution time * * @param $options cronInformation */ function execute($options){ if($options->firstTimeThisDay){ //do whatherver you like } } }
execute函数将会在系统cron被执行的时候执行,使用$options变量来获得上次执行的信息。
示例:ip_plugin_examples_cron.zip
测试Cron
用户可以通过访问http://www.example.com/ip_cron.php?test=1来测试执行Cron jobs。其中test变量是强制脚本在每一次访问该链接时都执行Cron,不管间隔时间是否有一小时,它强制所有的变量为真,从$options->firstTimeThisDay 到$options->firstTimeThisYear ,尽管上一次执行cron只在一分钟前。