- 创建一个类Test来执行Cron Job
<?php
/**
* Tony_SampleMinimal Module Of Magento 2
* Author: Tony
* Home : www.abmbio.xin
*/
namespace Tony\SampleMinimal\Cron;
use \Psr\Log\LoggerInterface;
class Test {
protected $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
/**
* Write to system.log
*
* @return void
*/
public function execute() {
$this->logger->info('Cron Works');
}
}
- 创建crontab.xml来配置Cron Job
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="custom_cronjob" instance="Tony\SampleMinimal\Cron\Test" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
- 启用Cron Job
php bin/magento cron:run
详情请看:Tony Blog