3

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?

3 Answers 3

7

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

2

Here's an example:

Create crontab.xml file in your custom module - app/code/Magenticians/Mymodule/etc and add the following code in it:

<?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="cron_name" instance="Magenticians\Mymodule\Cron\Run" method="execute">
<schedule>*/1 * * * *</schedule>-->
</job>
</group>
</config>

Note: I have scheduled it for one minute.

Now create Run.php file in app/code/Magenticians/Mymodule/Cron and add the following code in it:

class Run 
{
   protected $_logger;
   public function __construct(
       \Psr\Log\LoggerInterface $logger
   ) 
    {
     $this->_logger = $logger;
    }

   public function execute()
   {
       //Edit it according to your requirement
       $this->_logger->debug('Cron run successfully');
       return $this;
   }
}

Run the following commannds in your store root directory:

php bin/magento cache:flush
php bin/magento cron:run

Now go to var/log/debug.log and you will see the text Cron run successfully in it. You can also check new entry in cron_schedule table of database.

2
  • i ♥ u. I was stuck on schedule generation and cache:flush help me Jul 24, 2018 at 21:22
  • 1
    I'm glad that it helps you. Jul 27, 2018 at 10:49
1

To create a cron job for the Magento file system owner, enter the following command as a user with root privileges:

crontab -u <Magento file system owner user name> -e For example,

crontab -u magento_user -e A text editor displays. (You might need to choose a text editor first.)

* * * * * <path to php binary> <magento install dir>/bin/magento cron:run | grep -v "Ran jobs by schedule" >> <magento install dir>/var/log/magento.cron.log
* * * * * <path to php binary> <magento install dir>/update/cron.php >> <magento install dir>/var/log/update.cron.log
* * * * * <path to php binary> <magento install dir>/bin/magento setup:cron:run >> <magento install dir>/var/log/setup.cron.log

where

<path to php binary> is the absolute file system path to your PHP binary <magento install dir> is the directory in which you installed the Magento software; for example, /var/www | grep -v "Ran jobs by schedule" filters this message from the log, making any errors easier to spot

Source: http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-cron.html

0

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.