3

Setting up cron for a custom module what I believe I need to do is create a \etc\crontab.xml for the module.

What I also believe is happening is when the cron job magento cron:run runs all jobs in the table cron_schedule are executed (as long as time <= scheduled_at).

If this is correct, how does my job run_my_module for my custom module (my\module) get in cron_schedule? I've run magento cron:run manually but don't see my job in the table. What am I missing?

3

Schedule is generated periodically during magento cron:run execution. Period is defined separately for each cron group in the admin panel (Stores -> Configuration -> Advanced -> System -> Cron). New job will be scheduled only when Generate Schedules Every minutes pass after previous generation.

Implementation can be found here \Magento\Cron\Observer\ProcessCronQueueObserver::_generate

| improve this answer | |
  • Thanks @Alex! My group id is "default" and I see 2 settings for "Cron configuration options for group: default"; 1) "Generate Schedules Every" and 2) "Schedule Ahead for". Does "Generate Schedules Every" mean every time "magento cron:run" executes, it won't create a schedule for each job until after this "time"? Meaning if "Generate Schedules Every = 15", then a job won't be scheduled until 15 minutes has past? – user20719 Dec 30 '15 at 18:52
  • Yes. Until 15 minutes have passed after the latest schedule generation. – Alex Paliarush Dec 30 '15 at 19:14
2

Follow these steps:

Create crontab.xml file in your custom module - app/code/Magenticians/Mymodule/etc and paste the following code in it:

<?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="cron_name" instance="Magenticians\Mymodule\Cron\Run" method="execute">
<schedule>*/1 * * * *</schedule>-->
</job>
</group>
</config>

Now Create Run.php file in app/code/Magenticians/Mymodule/Cron and paste this code in it:

class Run 
{
   protected $_logger;
   public function __construct(
       \Psr\Log\LoggerInterface $logger
   ) 
    {
     $this->_logger = $logger;
    }

   public function execute()
   {
       //Edit it according to your requirement
       $this->_logger->debug('Cron run successfully');
       return $this;
   }
}

Go to Root directory using SSH and run the following commands:

php bin/magento cache:flush
php bin/magento cron:run

Go to var/log/debug.log and you will see the text Cron run successfully in it.

| improve this answer | |
  • Can possible to schedule cron without 'php bin/magento cron:run'? I use default group for my custom cron. – Dhaduk Mitesh May 17 '18 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.