Magento 2 – How to Create Cron Job for Custom Module

cronmagento2

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?

Best Answer

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

Related Topic