For some reason this code in module's config.xml:
<jobs>
<stockupdate>
<schedule><开发者_运维问答cron_expr>0 */2 * * *</cron_expr></schedule>
<run><model>stockupdate/cron::start</model></run>
</stockupdate>
</jobs>
doesn't work. Although after changing */2
to *
all seems to be alright.
To test it I first do a TRUNCATE cron_schedule
, then clean the cache rm -rf var/cache
then run php cron.php
and then SELECT * FROM cron_schedule
to see if job was scheduled.
I know there is also the nasty way 0 0,2,4,6,8,10,12,14,16,18,20,22 0 0 0
but this is a very inelegant solution.
So how I can tell Magento to run this task every 2 hours?
You could use
<schedule><cron_expr>0 * * * *</cron_expr></schedule>
and have your code check for even hours. Not elegant, but it would work. If it needs to be configurable you could add a configuration option to control how often it runs in hours.
Update:
Look at Mage_Cron_Model_Schedule
in the matchCronExpression
method:
// handle modulus
if (strpos($expr,'/')!==false) {
$e = explode('/', $expr);
if (sizeof($e)!==2) {
throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting 'match/modulus': ".$expr);
}
if (!is_numeric($e[1])) {
throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting numeric modulus: ".$expr);
}
$expr = $e[0];
$mod = $e[1];
} else {
$mod = 1;
}
// handle all match by modulus
if ($expr==='*') {
$from = 0;
$to = 60;
}
// handle range
elseif (strpos($expr,'-')!==false) {
$e = explode('-', $expr);
if (sizeof($e)!==2) {
throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting 'from-to' structure: ".$expr);
}
$from = $this->getNumeric($e[0]);
$to = $this->getNumeric($e[1]);
}
// handle regular token
else {
$from = $this->getNumeric($expr);
$to = $from;
}
if ($from===false || $to===false) {
throw Mage::exception('Mage_Cron', "Invalid cron expression: ".$expr);
}
return ($num>=$from) && ($num<=$to) && ($num%$mod===0);
It is setup to handle */2 properly.
for every 2 hour use expression
<schedule><cron_expr>0 */2 * * *</cron_expr></schedule>
if you set
<schedule><cron_expr>* */2 * * *</cron_expr></schedule>
you will get, that task will be sheduled every minute each second hour. 60 tasks for second hour, 0 tasks for 3d hour, 60 tasks for 4 hour etc.
Try setting the cron task to * */02
If you want every 2 hours, the expression would be:
<schedule>00 */2 * * *</schedule>
This will execute at minute 0 past every 2nd hour.
Check for more expressions
精彩评论