How to Setup Cron Job in Magento 2?

How to Setup Cron Job in Magento 2

Some of the Tasks Which are Handled by Cron in Magento 2

Catalog price rules
Newsletters
Generating Google Sitemaps
Customer Alerts/Notifications
Re-indexing
Automatic updating of currency rates

Now We will Going to Setup Cron on the Server

Run the following command from the terminal

crontab -e

If you are logged in as root user you can specify the cron job for the Magento file system owner only by specifying the username by using the -u switch.

crontab -u -e

When you hit the above command, editor option will appear select anyone in which you are comfortable with. In the editor enter the following cronjob.

* * * * * /bin/magento cron:run

In our case we have used the following command.

* * * * * php /home/ayaz/www/html/mage2/bin/magento cron:run

We are done on the setup for server. Please note that the cronjob I setup is running every minute. You can schedule it as per your need. You can check the cron has setup correctly by viewing the cron_schedule table in your magento database. You will see entries like this:

##################################################

Read Also: How To Migrate From Magento 2.2 To Magento 2.3

3 Types of Cron are Introduced in Magento 2 as Given Below

First of we will login in server (ubuntu) and open crontab by below command and add the cron as our requirement
command = crontab -e

*/1 * * * * php -c /path/to/magento2/bin/magento cron:run
*/1 * * * * php -c /path/to/magento2/bin/magento/update/cron.php
*/1 * * * * php -c /path/to/magento2/bin/magento setup:cron:run

 

*/1 * * * * php -c /path/to/magento2/bin/magento cron:run
*/1 * * * * php -c /path/to/magento2/bin/magento/update/cron.php
*/1 * * * * php -c /path/to/magento2/bin/magento setup:cron:run

But we do not need to configure all of them,

1st cron command will work for re-indexes indexers, sends automated e-mails, generates the sitemap, and so on.
Rest 2 cron commands are used by the Component Manager and System Upgrade.

To configure cron, edit crontab command line Code:

crontab -u magento_user -e
crontab -u magento_user -e

Add this line at the end of file

* * * * * php -c /path/to/magento2/bin/magento cron:run
* * * * * php -c /path/to/magento2/bin/magento cron:run

This will run every minute. If you do not want to run every minute you can set every 5 min as given below:

*/5 * * * * php -c /path/to/magento2/bin/magento cron:run
*/5 * * * * php -c /path/to/magento2/bin/magento cron:run

How useful was this post?

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *