setup-Magento-Cron-Job-Magento

Magento Cron Job Setup | Tutorial 2020

Posted by

If you are already active on Facebook or Youtube, you should have known those social media all have the same feature that can schedule the timeline publicity posts that you have previously prepared.

However, can you apply that effective working system into your business, specifically your Magento 2 platform Website?

The answer is here, thanks to Cron Jobs; you will save a great deal of time to manage the server and related tasks.

If you’re an office worker, you can now relax after an 8-hour day spent in the office instead of spending your evening backing up files and managing contacts.

At the same time, you do not need to try to remind yourself to recreate periodic tasks, thanks to the Magento 2 Cron Job Setup.

What is a Cron Job?

Are you wondering what a Cron Job means according to its miracle?

Hence, Cron Job is a function used to periodically perform specific commands for a period predetermined by the administrator.

Put it simply, Cron Job is a function that helps you automatically perform specific tasks, which have been formerly set up.

cron-job-definition

The Cron Job is an excellent periodical method to make any of your essential programs active automatically and accurately instead of doing them manually.

Because of its functionality, Magento uses the Cron Job for numerous features to schedule activities. The operations you can control scheduling from Magento include:

  • List price rules
  • Newsletters popup
  • Forming Google sitemaps
  • Customer Cautions/Notifications (product price, product stock status)
  • Reindex available
  • Private sales (Magento Commerce only)
  • Updating currency rates
  • E-mails (including order confirmation and transactional)

Based on the rapidly changing speed of the trade balance, take the example of Currency Rate Updates, is it too complicated to manually update it regularly (maybe monthly but also daily)?

For another example, without a Magento Cron Job Setup, you have to refresh the catalog price rules several times a day manually. If you have a large scale company, this can be a time-consuming task. Furthermore, what if you disremember to do it?

Do You Need Magento 2 Cron Job Setup?

magento-2-cron-job-setup

When Magento 2 with Cron Jobs, there are some improvements compared to Magento 1.x version.

The database doesn’t seem to be more advanced over Magento 1, but the configuration does.

What’s new in the configuration is that now we can group our tasks and choose whether we want jobs to be executed in parallel or not as separate processes, which could be very helpful sometimes.

As a result, the Cron Jobs list you created will now in a separate file called Crontab, which is not available in Magento 1.x.

Magento-update

Magento 2 With “Cron” Terms

The difference between Cron, Cron Job, and Crontab

cron-cronjob-crontab
  • Cron: the name of the tool
  • Crontab: generally the file that lists the jobs that Cron will execute
  • Cronjob: a specific set of execution instructions specifying day, time and command to execute

On most systems, you must get approval from the system administrator to submit Cron Job requests to Cron.

On some shared systems with only one Crontab file, only the leader has the authority to access the Crontab command.

Magento 2 Crontab

Starting with version 2.2, Magento creates a Crontab for you. The Magento 2 Crontab is the configuration used to run Magento Cron Jobs.

Put this term in a more straightforward way to comprehend:

  • Cronjobs are commands that execute prepared actions at a given time
  • Crontab is where storing Cron Job data.
magento-2-crontab

According to Magento’s guidance, it uses Cron for two sets of tasks with two different configurations:

  • PHP command-line configuration: The general Cron Job that reindexes indexers, generates e-mails, generates the sitemap, etc.
  • Web server PHP plug-in configuration: Two other Cron Jobs are used by the Component Manager and System Upgrade utilities.

A Cron Job Operation

Typically, Cron Jobs has fixed components as follows:

  • The script to be called or executed.
  • Command to execute the script on a reoccurring basis (usually set up in cPanel)

The operation of the text depends on what the text is called and executed. Typically, scripts called Cron Job will modify the files or databases.

magento-2-cron-job-setup-warns

Besides, Magento official developers recommended that you should run Cron Job as the Magento file system owner instead of run Cron as “root” (custom available installation bypasses the developers’ high-security barriers) or run Cron as the Website server user.

Magento 2 Cron Job Setup Step-by-Step

More tutorial should you read:

There to say, Magento Cron Job Setup is slightly different from Magento 2 Cron Job Setup as Magento version 1.x runs Cron in a different system. Otherwise, on the Magento 2.x platform, generating a Crontab file is necessary before creating Cron Jobs.

To remind you, below is a Magento 2 Cron Job Setup. As a result, if your Website is running on the previous Magento 1.x version, there will be a different tutorial for you to follow.

 

STEP 1: Crontab Opening

To open the Crontab as the user, type:

crontab -e

To open the Crontab as a specific user, type:

crontab -u USERNAME -e

In the above example, you will change the “USERNAME” with your reference.

STEP 2: Cron Job Generating

Magento 2 Cron Job setup with command line

magento-2-cron-job-setup-command-line

Before start trying, you have to notice that Magento creates three log files, which are:

var/log/magento.cron.log
var/log/update.cron.log
var/log/setup.cron.log. 

You can watch these log files and run the following commands on your command line:

#~ MAGENTO START c5f9e5ed71cceaabc4d4fd9b3e827a2b
* * * * * /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 c5f9e5ed71cceaabc4d4fd9b3e827a2b

Magento Cron Job setup with WGET Command

magento-2-cron-job-setup-WGET
Following command:
*/5 * * * * wget -q http://www.yourdomain.com/cron.php

In the above example, you will need to change the link to point the cron.php file in your Magento root.

STEP 3: Magento Cron Job Setup with WGET Testing

This method will testify whether your Magento Cron Jobs are set up correctly or not without waiting a couple of days.

With the Magento 2 Cron Job setup with WGET, change your Crontab command to the following line:

*/5 * * * * wget-bad-command -q http://www.yourdomain.com/cron.php

Setup-error

Notice that an error has appeared in this method.  In the above link, the path to the cron.php to a file that doesn’t exist.

You need to change that email address to your email address.

Now, if your Cron Job Setup is correct, every five minutes, you should get an email saying that there was an error with the Cron script. Once you have that email, you can safely revert to the correct version.

If you don’t receive this email, your Magento 2 Cron Job Setup did something wrong. Check it carefully or comment below to let us know!

Magento Cron Job Setup With Magento Extension

More explore:

In Magento 2 default, it is inconvenient for administrators to manage Cron tasks because it requires the store database. It is unrequired to go into the database whenever any errors of Cron Jobs occurred.

Also, accessing databases is quite risky and requires absolute priority as well as strong technical skills.

Therefore, designers have launched an updated specialized Magento Extension for Cron Job. Here is the one by Mageplaza which you should give it a try!

Mageplaza Magento 2 Cron Job Setup (FREE)

mageplaza-cron-job-setup

With Mageplaza Magento Cron Job Setup Extension, admins can manage all the executed Cron Jobs through the listing grid.

The grid allows supervising Cron Job details, including Cron Job code, group name, activation status, job method, instance classpath, schedule, etc.

Also, at the Manage Cron Jobs Grid, store administrators can take mass actions as delete, change status, or execute all or selected Cron tasks conveniently and in a time-saving way.

Magento Cron Job Setup in Magento 2 Admin with Mageplaza

After installing the Mageplaza Magento Cron Job Setup Extension, you have a permit to skip all the complicated and time-consuming installation steps above and will finish the installation step with just one-click.

With this Extension, you can install Cron Jobs right from Magento’s Admin editing page, followed this below guidance.

STEP 1: Login Magento 2 Admin

  • On the Admin page, click Stores.
  • In the Settings, select Configuration
  • Open the Cron section.
mageplaza-cron-job-setup-step-1

STEP 2: Fill in the blank

Open these 2 sections:

  • Cron configuration options for a group: Index
  • Cron configuration options for a group: Default

Complete all following information:

mageplaza-cron-job-setup-step-2
mageplaza-cron-job-setup-step-2-2

Consequently, all of your tasks have been set up and will automatically do their mission!

Conclusion

In the bottom line, I have to admit that there is too much information for users to comprehend. Moreover, standing in front of a vast knowledge can make you confused about whether to follow those guidances or not.

Accordingly, we have summarized the information from other articles in detail and reasonably paraphrase them to understand.

Hopefully, after reading this article, you can understand more about Magento Cron Job Setup. We would appreciate it when you found this article helpful. Please let us know your feedback!

Besides, there are still many other informative articles waiting for you to read:

Leave a Reply

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