– group_id : a group of cron job, I will explain it later on this article
– name : cron name, it must be unique
– instance : the class which will be called
– method : the method which will be called
– schedule : the schedule of our cron with Cron Format
– config_path : You can put this instead of schedule tag in order to set the schedule on Magento 2 config, we will do that later on this article

We declared a class on the XML, we will create it !
Add the following file :app/code/Maxime/Jobs/Cron/DisableJobs.php

We will not test the content of this method. We will create an unit test later in the training to check if the code is ok.

Don’t check if the cron creation is ok, continue to read the lesson, we will do more tests later on this page

Check scheduled cron

Magento 2 launch it’s cron every minutes.
During this call, it will check every crontab.xml files in order to schedule the next cron job to launch.

Where can you find its ?

We will find the list inside the table cron_schedule

You will see :
– The job code set on the XML
– Status of the job
– Messages of the job. It can contain an exception message if the job failed
– Creation date of the job
– Scheduled date of the job
– Beginning date of the job
– End date of the job if is OK

Cron group with Magento 2

As you can see, we defined a group_id on the XML file.
Magento has got 2 groups :
– default (Magento run it every 15 minutes by default, so if you schedule a task every minutes, it will be really run every 15 minutes)
– index (Magento run it every minutes)

You can put your job inside one of these groups.
But what is the goal of these groups ?
With it, you can run parallel tasks.

If you have a long cron job, it will run without lock the other jobs.

So the 2 natives groups allow Magento to run tasks and reindex at the same time.

Nice article. A few tips for anyone else new to cron in general; cron also allows you to use values 0-6 (Sunday-Saturday) for the day column. You could also use the more human-friendly version and just write mon, tue, wed, etc…
0 -> Sun
1 -> Mon
2 -> Tue
3 -> Wed
4 -> Thu
5 -> Fri
6 -> Sat
7 -> Sun

The following example would run “command” at midnight every monday, tuesday, and friday:
0 0 * * mon,tue,fri command

Thanks for your article. I’m testing your cron code in Magento 2.1.8.. it work fine if I set the tag “schedule” in /etc/crontab.xml, but it don’t work if i set the schedule by config_path. If i try to do it, the cron don’t run and if I verify the list of crons (with n98-magerun2) i see the cron but in the schedule section i see a list of dashes.