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

Nov 12th, 2018 2 min to read

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! 🙂

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.