Magento – Can someone give detailed explanation about scheduling in Magento 2 cron job

cronmagento2

I am using Magento CE 2.0.0.

I set up cron job for my custom task but I could not make it run every 15 minutes.
So can someone provide detailed explanation about how to correctly set up schedule?

  1. Firstly, there is a configuration for cron in admin:

admin cron configuration

This parameters are for generating "cron_schedule" table in database.
How these parameters affect on my task scheduling (I'm using group "default")?

  1. On server I must set up task in crontab to run magento cron:run.
    So in my case it (crontab -l):

*/15 * * * * /usr/bin/php /srv/www/my_directory/bin/magento cron:run

In magento devdocs there is the note:
devdocs note
What does it mean? Do I have to include my command twice in crontab? For example:

*/15 * * * * /usr/bin/php /srv/www/my_directory/bin/magento cron:run; /usr/bin/php /srv/www/my_directory/bin/magento cron:run

  1. Lastly, there is a cron job configuration in Magento 2 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="my_custom_job" instance="My\Module\Cron\MyCustomJob" method="execute">
            <schedule>*/15 * * * *</schedule>
        </job>
    </group>
</config>

I set it to run every 15 minutes.

So how to put all these things together?
I want to run my custom task every 15 minutes.

Here is the debug.log:

> [2016-09-23 12:00:03] main.DEBUG: MyCustomTask {"is_exception":false} []
> [2016-09-23 12:15:03] main.DEBUG: MyCustomTask {"is_exception":false} []    
> [2016-09-23 13:00:03] main.DEBUG: MyCustomTask {"is_exception":false} []
> [2016-09-23 13:30:03] main.DEBUG: MyCustomTask {"is_exception":false} [] 
> [2016-09-23 14:45:03] main.DEBUG: MyCustomTask {"is_exception":false} []
> [2016-09-23 16:00:03] main.DEBUG: MyCustomTask {"is_exception":false} []
> [2016-09-23 16:45:03] main.DEBUG: MyCustomTask {"is_exception":false} []

Best Answer

This can not be a complete answer to my question about detailed explanation of how Magento's cron schedule works and admin config parameters, but there are few notices:

  1. System cron schedule must be more frequent then magento's cron job schedule. In other case it will skip tasks.
  2. In case of above notice there is no need to include bin/magento cron:run command in system cron twice.
  3. As for magento docs it's recomended to run default magento's cron tasks every minute.
  4. There is also config for cron groups in admin panel in which there is a "Generate Schedules Every" parameter. It affects on cron job launch frequency (job can not launch more frequent than this parameter specifies). What for "Schedule Ahead For" it's a mystery for me for now.

Therefore, for example, if I set system cron to run every minute:

* * * * * /usr/bin/php /srv/www/my_directory/bin/magento cron:run

set up cron job (which is in default group) to run every 5 minutes:

> <group id="default">
>     <job name="my_custom_job" instance="My\Module\Cron\MyCustomJob" method="execute">
>         <schedule>*/5 * * * *</schedule>
>     </job>
> </group>

and in admin config panel leave default values ("Generate Schedules Every" 15 minutes),

than my cron job will run every 15 minutes (as admin config limits).

For more frequent launching I must create custom group with other parameters.

Related Topic