Magento 2 – How Cron Job Works

cronmagento2

I just learned about how the cron job works in Magento 2. I have created a simple job and i put
* * * * * (at every minute) in <schedule> tag in crontab.xml. I enter bin/magento cron:run in terminal, my job is saved into cron_schedule table. But my job is not working. Then i tried to run the job again after a while, my job is works.

What i want to ask, isn't my job should automatically run every minute? Why i should run it manually in terminal?

Best Answer

Configure Magento cron job scheduler

By defining <schedule> in crontab.xml, you're setting up periods when Magento should schedule that process. To be able to schedule a job, you have to run Magento Cron scheduler - and that's what you did. But, if you want to run your cron job in defined period continuously, you'll have to configure Magento Cron scheduler to be run in the background.

In order to do that, you'll have to insert following lines into your cron. * * * * * <path to php binary> <magento install dir>/bin/magento cron:run * * * * * <path to php binary> <magento install dir>/update/cron.php * * * * * <path to php binary> <magento install dir>/bin/magento setup:cron:run

<path to php binary> most often is /usr/bin/php5 (you can find it by typing "whereis php5" or "whereis php" in command line).

<magento install dir> is path of your Magento instance.

More information how to set cron jobs can be found at http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-cron.html.

Background on Magento cron jobs implementation:

There are cron jobs defined at the system level, and they can be considered as real cron jobs. You can check those cron jobs by typing "crontab -e" in the command line for the current user.

On the other side, Magento offers the sophisticated configuration of cron jobs, but they are not "real" cron jobs. They are all invoked programmatically through one real cron job, by executing "bin/magento cron:run".

So, when you execute:

* * * * * <path to php binary> <magento install dir>/bin/magento cron:run

this is what is happening in Magento cron job scheduler (pseudo-code):

...
// runs all scheduled jobs (until limit is reached, configurable in Magento Admin)
foreach ($pendingCronJobs as $_job) {
    $_job->run();
}

// generate new schedules for upcoming cron jobs
$this->generateNewSchedule();

// clean up old entries
$this->cleanUpOldEntries();
...

If you want to check more thoroughly how it looks behind the scenes, check Magento\Cron\Observer\ProcessCronQueueObserver, method execute()

Cron job groups

As you can see from Magento\Cron\Observer\ProcessCronQueueObserver, all scheduled cron jobs will be executed sequentially, one by one. It means that cron job (which is in "execution" state) will block the others from being executed until it's finished.

This can make troubles, especially if you have some long running cron jobs.

Magento2 offers the possibility to configure different groups for cron jobs. On that way, you can split cron jobs by putting long-running cron job into separate group(s).

More information about cron groups can be found at:

devdocs.magento.com/guides/v2.0/config-guide/cron/custom-cron-ref.html

Related Topic