Chapters Close

How to setup and remove cron using CLI in Magento 2?

This article will help you to understand how to create or remove your Magento Crontab.

Create Cron using CLI

Starting with version 2.2, Magento creates a crontab for us. Run cron as the Magento file system owner.

  1. Log in as, or switch to, the Magento file system owner in the terminal.
  2. Change to your Magento installation directory: cd <your Magento install dir>/
  3. Enter the following command: php bin/magento cron:install [--force] Here, use –force to rewrite and Magento crontab.

The Magento crontab is inside #~ MAGENTO START and #~ MAGENTO END comments in your crontab.

  • magento cron:install does not rewrite an existing crontab inside #~ MAGENTO START and #~ MAGENTO END comments in your crontab.
  • magento cron:install --force has no effect on any cron jobs outside the Magento comments.

To view the crontab, enter the following command as the Magento file system owner in terminal:

Example:

#~ MAGENTO START
* * * * * /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
* * * * * /usr/bin/php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log
#~ MAGENTO END

Remove Cron using CLI

To clear cron schedule in Magento 2:

  1. Log in as, or switch to, the Magento file system owner in the terminal.
  2. Change to your Magento installation directory: cd <your Magento install dir>/
  3. Enter the following command: php bin/magento cron:remove

This command has no effect on cron jobs outside the #~ MAGENTO START and #~ MAGENTO END comments in your crontab.

Run Cron from CLI

Command options:

php bin/magento cron:run --group index
php bin/magento cron:run --group default

php bin/magento cron:run [–group=""] where --group specifies the cron group to run.
Example :

Hope it helps you! 🙂 Enjoy your magento development activities.

FAQs

What is * * * * * In cron job?


The * * * * * in cron job indicates the run schedule of the job. Each asterisk corresponds to the minute, the hour, the day, the month, and the year at which the job should repeat. You can replace each of the * with a number (0-59 for minutes, 0-12 for hours, and so on) to create a schedule for the job. If you use the expression “* * * * *” the job will run every single minute of every hour.

How do I disable cron job in Magento?


To disable the cron job in Magento 2, use the command line – php bin/magento cron:disable. It will update the cron_schedule table and set the status field to error for all scheduled cron jobs. Alternatively, you can run the command crontab -e and comment out the line that starts with * * * * * and ends with php.

How to create custom cron in Magento 2?


To create a custom cron in Magento 2, follow the below-mentioned steps –

1. Create a new module in the app/code directory of your Magento installation.
2. Next, create a new crontab.xml file in the module’s etc directory.
3. In the above file, use the following code to define your custom cron job:
<?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="vendor_module_custom_cron" instance="Vendor\Module\Cron\CustomCron" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>

4. Further, create a new class in the Cron directory of your module as CustomCron.php.
5. Now, in the above file, define the execute method by adding the below code –
<?php
namespace Vendor\Module\Cron;
class CustomCron
{
public function execute()
{
//Add your cron job logic here
}
}

6. Thereafter, run the command php bin/magento setup:upgrade in your Magento root directory to enable the module.

How to check cron is enabled in Magento 2?


To check cron is enabled in Magento 2, use the command line – php bin/magento cron:status in CLI. Make sure to run it in your Magento root directory! This command will return the status of the cron jobs and will only indicate whether or not cron is enabled.

Speak your Mind

Post a Comment

Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Grow your online business like 2,050 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
    envelope

    Thank You!

    We are reviewing your submission, and will be in touch shortly.