8

System cron starts Magento 2 cron every minute:

$ crontab -l
*/1 * * * * /usr/bin/php /home/user/prj/mage2/bin/magento cron:run >> /home/user/prj/mage2/var/log/cron.log

I see new line Ran jobs by schedule. is added every minute into the log.

This is my settings for my task:

<?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="my_own" instance="\Vendor\Module\Cron\Task" method="execute">
            <schedule>...</schedule>
        </job>
    </group>
</config>

These settings run my job every 15 minutes in fact (my own message is added every 15 minutes into the log):

<schedule>* * * * *</schedule>
<schedule>*/1 * * * *</schedule>

This is my log:

...
Ran jobs by schedule.
Ran jobs by schedule.
Ran jobs by schedule.
My own job is started.
Ran jobs by schedule.
...

These are Magento 2 cron settings: Magento 2 Cron settings

How can I set <schedule> to run my task every 1 minute?

6
  • This should works fine <schedule>* * * * *</schedule>
    – Jackson
    Feb 22, 2017 at 6:09
  • In fact <schedule>* * * * *</schedule> runs my task every 15 min :(
    – Alex Gusev
    Feb 22, 2017 at 6:33
  • Something is wrong on ur server. It should work globally as 1 minute.
    – Jackson
    Feb 22, 2017 at 6:38
  • Yes, it is :( System cron (linux) starts every minute and launches Magento 2 cron every minute but Magento cron launches my own task just once in 15 min for <schedule>* * * * *</schedule> Probably, there are some Magento 2 configuration settings that prevent launch tasks more, then once in 15 min?
    – Alex Gusev
    Feb 22, 2017 at 6:43
  • Check in cron_schedule table
    – Jackson
    Feb 22, 2017 at 6:47

6 Answers 6

9

There are 2 groups in Magento 2 cron: index & default. Tasks/jobs are placed into index group will start every 1 minute (by default setup):

<group id="index">
    <job name="..." instance="..." method="...">
        <schedule>* * * * *</schedule>
    </job>
</group>

Tasks/jobs are placed into default group will start every 15 minutes (by default setup):

<group id="default">
    <job name="..." instance="..." method="...">
        <schedule>* * * * *</schedule>
    </job>
</group>
3
  • Is there any configuration settings for these?
    – mjdevloper
    Mar 20, 2017 at 12:48
  • Stores / Configuration / Advanced / System / Cron (Scheduled Tasks) / Cron configuration options for group: [index|default]
    – Alex Gusev
    Mar 21, 2017 at 7:36
  • i have setup group id index for every minutes and alredy run cron:run command but its not working for me Jul 14, 2018 at 9:50
8

It appears that cron jobs in Magento 2 are limited by their settings defined in the cron group they belong to. I'm not sure if this is a bug or a feature.

Alex Gusev already points this out in his answer, but to clarify it a bit more: There are 2 cron groups by default in Magento 2: default and index. These are defined in Magento_Indexer/etc/cron_groups.xml and Magento_Cron/etc/cron_groups.xml. For example:

<group id="default">
    <schedule_generate_every>15</schedule_generate_every>
    <schedule_ahead_for>20</schedule_ahead_for>
    <schedule_lifetime>15</schedule_lifetime>
    <history_cleanup_every>10</history_cleanup_every>
    <history_success_lifetime>60</history_success_lifetime>
    <history_failure_lifetime>600</history_failure_lifetime>
    <use_separate_process>0</use_separate_process>
</group>

These cron group determine the default / global values of every cron job. I'm not sure how it works but according to my own tests you cannot set a cron to run more often per hour than determined in the schedule_generate_every-setting. So for example, if you have something like this:

<group id="default">
    <job name="custom_module" instance="Custom\Module\Cron\DoSomething" method="execute">
        <schedule>*/1 * * * *</schedule>
    </job>
</group>

Even though you might expect that this cronjob is scheduled every minute, it's global settings limit it to a minimum timeframe of 15 minutes.

To solve this, you can either:

  • Edit the setting in the system configuration (Advanced → System → Cron), or:
  • Override the global default in your module by adding a cron_groups.xml-file of your own (assuming that the client did not already tinker with the configuration settings):

Example config_groups.xml-file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"            xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
    <group id="default">
        <schedule_generate_every>1</schedule_generate_every>
    </group>
</config>

Bug? Feature? Misleading configuration setting? I'm not sure how to call this. You could argue that it's a good thing that you have a global scope that can limit 100+ modules to try to run a cron every minute. On the other hand: If I'm a developer who knows what he's doing, there might be good reasons to run every minute.

If running every minute is mandatory it might also be an idea to add a whole new cron group for your configuration (don't pollute the index-cron group with non-indexing cron jobs). But that's a matter of architecture and I'll leave that up to you.

2
  • 1
    I don't think that is correct. The schedule_generate_every tells Magento to update the schedule for this cron group every 15 mins. If you set a job to run every minute in that group then Magento would generate 15 cron_schedule entries for that job, one for each minute. Apr 20, 2018 at 13:41
  • I would expect that too, but these are the results I observed at the time. I'm not even sure which Magento version it was anymore. 2.0? 2.1? Results may differ in the meanwhile. I was also questioning whether this was a bug or a feature. Apr 23, 2018 at 7:59
4

To schedule once per minute, need to give

* * * * *

From Left to right, 1st star represents Minute(range: 0-59) 2nd represents Hour(range: 0-23) 3rd represents Day of the Month (range: 1-31) 4th represents Month of the Year (range: 1-12) 5th represents Day of the Week (range: 1-7, 1 standing for Monday)

Refer Link for cron timing : http://www.nncron.ru/help/EN/working/cron-format.htm

Reference Link for setting custom cron : http://www.dckap.com/blog/how-to-set-and-configure-custom-cron-jobs-in-magento-2/

2

It's not clear from your question which schedule you want, so here are both:

Every 1 minute:

* * * * *

Every 15 minutes:

*/15 * * * *

You may find this tool useful for creating/debugging cron schedules: https://crontab.guru/

4
  • I have added an "echo" statement in my task and echoed message appears every 15 min for "* * * * *" schedule. So, in fact this settings runs my task every 15 min., not every minute.
    – Alex Gusev
    Feb 22, 2017 at 6:37
  • Check your configuration under Stores > Configuration > Advanced > System > Cron (Scheduled Tasks). Make sure the schedule settings are OK for your setup. Feb 22, 2017 at 6:47
  • All settings are unchanged (set by default).
    – Alex Gusev
    Feb 22, 2017 at 6:50
  • Hi Danny Nimmo, Its working when i am running cron:run command. That's mean every time i am running cron:run command than its working Jul 17, 2018 at 11:34
0

Please Try

*/1 * * * *

for scheduling cron at each minute. You can check your cron entries in the cron_schedule table to see the scheduling entries done by Magento.

0

Please add * * * * * in your schedule tag in xml file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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