4

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} []

2 Answers 2

2

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.

2
  • Jurgen, I have a custom group with 2 jobs, they must run once a day. I have :one <schedule>0 3 * * 1-5</schedule> (only from Monday to Friday) and another: <schedule>0 8 * * 1-5</schedule>. I only want that my jobs run once a day, on my system cron schedule, what is the value that should go to: Generate Schedules Every, do you know?
    – mvistas
    Dec 20, 2016 at 12:21
  • mvistas, I think you can put your two jobs in default group. They will run with schedules that you specified (once a day). System cron runs magento's cron (once a minute - needed for magento's other tasks) and magento's cron then checks it's own schedule. "Generate Schedules Every" is the interval of refreshing schedule table in database. So in your case it could be anything less than 1 day. Therefore there is no need to put jobs in separate group.
    – jurgen
    Dec 20, 2016 at 17:47
0

Navigate to Store->Configuration->Advanced and system.There you can set the time interval for cron job.You can run the cron job by command line

php bin/magento cron:run;

Cron will only run if its setup on the server.So before doing anything else make sure that cron is configured on the server or not.

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.