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

By: Saurabh Parekh, Nov 12th, 2018 # Magento 2 # Backend # How to

How to setup and remove cron using CLI?

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:

crontab -l

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 remove the Magento crontab:

  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=""] where --group specifies the cron group to run.
Example :

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

Hope it helps you! :)