Skip to content

Instantly share code, notes, and snippets.

@Bat-Chat
Last active October 29, 2018 07:32
Show Gist options
  • Save Bat-Chat/c71ef692bd97b2e05f99f1beb820fae1 to your computer and use it in GitHub Desktop.
Save Bat-Chat/c71ef692bd97b2e05f99f1beb820fae1 to your computer and use it in GitHub Desktop.
createCron

We need two files below

app/code/BatChat/Module/etc/crontab.xml

<?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="batchat_module_cron_cronclass" instance="BatChat\Module\Cron\CronClass" method="execute">
            <schedule>* */3 * * *</schedule>
        </job>
    </group>
</config>

app/code/BatChat/Module/Cron/CronClass.php

<?php

namespace BatChat\Module\Cron;

class CronClass {
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * CronClass constructor.
	 *
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    /**
     * Description
     *
     * @return void
     */
    public function execute()
    {
        $this->logger->info("Cron job running");
    }
}

Run cron from the command line

bin/magento cron:run [--group="<cron group name>"]

https://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-cron.html https://devdocs.magento.com/guides/v2.2/config-guide/cli/config-cli-subcommands-cron.html

DB cron request

SELECT * from cron_schedule WHERE job_code like '%job_name%';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment