0

I have a question about cron jobs in Magento. I have a Magento 2 site already installed.

By running this database query:

select job_code from cron_schedule GROUP BY job_code

I get several cron jobs listed, however, if I list all cron jobs in Linux using crontab -l, Magento cron jobs are not there, so I am wondering how Magento 2 runs cron jobs. It seems Magento cron jobs have nothing to do with operating system cron jobs.

Does Magento launch a thread somewhere in the code and that thread runs the jobs?

Thanks Jaime

1
  • 1
    Magento has own cron functionality/module .so, in lunux cron you just need to set "php bin/magento cron:run" command it will handle all magento cron defined in magento. @sdaugherty explained more detailed Jul 6, 2021 at 4:21

1 Answer 1

1

Yes, you're right. The system doesn't know about all of the cron jobs defined inside Magento modules. They get exposed/scheduled/executed by a single system level crontab entry that runs the cron:run Magento command.

System Level Cron

With Magento, you will have one system level cron job that kicks off cron:run for Magento. This command is responsible for scheduling and executing cron jobs defined inside enabled modules.

Whenever it runs (usually every minute) it will schedule ahead (by adding records to the cron_schedule table) module cron jobs based on the magento cron group settings used.

It will also execute jobs in the cron_schedule table that have a schedule that falls within the correct window of time.

Example System Level Crontab Entry

* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log

Software Level Cron Groups/Jobs

Each Magento module has the ability to define software level cron jobs via the use of etc/crontab.xml and etc/cron_groups.xml

Cron Groups

Below is an example of the default cron group that comes with Magento out-of-the-box. You do not need to add this file to your module unless you need custom control over the scheduling settings for a group of jobs. If you do need the control, you would add the file to your module and specify a new/unique group id.

Read this for more info:

Magento DevDocs - Specifying cron group options

[Vendor]/[ModuleName]/etc/cron_groups.xml

<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>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>4320</history_failure_lifetime>
        <use_separate_process>0</use_separate_process>
    </group>
</config>

Cron Jobs

This is how you would define a cron job inside of a module. If you had defined a new cron group with custom settings, you would specify that group id when adding a job in this file. The name you give the job is what shows up for the value in cron_schedule.job_code. The most basic way to specify a schedule is to use the <schedule/> node. There are other options for specifying a schedule, which can be seen in the link below.

Read this for more info:

Magento DevDocs - Configure cron groups (their jobs)

[Vendor]/[ModuleName]/etc/crontab.xml

<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="example_cron_job_code" instance="Vendor\YourModule\Cron\ExampleCronJob" method="execute">
            <schedule>*/5 * * * *</schedule>
        </job>>
    </group>
</config>

References

Magento DevDocs - Configure and run cron

Magento DevDocs - Specifying cron group options

Magento DevDocs - Configure cron groups (their jobs)

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.