Cron is a job scheduler on UNIX OS. It is time-based. Thus, it can enable you to arrange shell scripts or commands to run in certain periods or time. It’s usually used to automate system administration or maintenance. So, if you need to schedule a certain task to be repeated, cron is exactly what you need.
Magento 2 Cron Job
If you want Magento 2 to work properly, make sure you enable Cron jobs. With cron job, you won’t need to refresh your Magento catalog index every two minutes any longer.
Magento cron job allows to schedule the following activities to regularly occur in the future:
- sending newsletters,
- cataloging price rules,
- generating Google Sitemaps,
- sending customer alerts or notifications,
- updating currency rates,
- scheduling DB logs cleanup,
- managing Magento e-mails,
- organizing private sales.
Failure to properly setup cron job Magento 2 will cause these features to malfunction or not function at all.
Cron Configuration Options
If you want to set up configurations for this section, you will need to log in the Admin Panel first.
Then, go to Stores > Settings > Configuration:
In the Configuration section, go to Advanced right at the bottom, and click System:
You will arrive at:
Within these cron configuration options, you will be able to set up:
• Generates Schedules Every. This is the frequency of future jobs in minutes.
• Schedule Ahead of. Being added to cron_schedule database queue, this is the time in minutes for jobs to be scheduled in the future.
• Missed if not Run Within. This is a period time in minutes past the scheduled time that allows jobs to be still brought to life before their status is changed to missed.
• History Clean Up Every. This is a period of time in minutes when a cleanup will be performed.
• Success History Lifetime. This is a period of time in minutes after which a record is made when a job is a success.
• Failure History Lifetime. This is a period of time in minutes after which a record is made when a job is a failure.
• Use Separate Process. This is probably one of the most essential options to consider as it creates a separate process for a selected group when flagged.
Setting Magento Cron Job on UNIX
If you are using UNIX OS, it will automatically recognize requests from Magento to use the cron tab service.
The cron.php file is located inside the Magento directory. Use this to manage and dispatch cron jobs. The cron.php script will execute the internal Magento cron job manager, sending commands when the cron jobs are to be executed.
It must have such a look:
Setting Cron Job Magento 2 using cPanel
Setting up Magento 2 cron job cPanel can be helpful if you don’t see the cron tab. Make sure you have not yet set up another cron job calling cron.php file in Magento. Otherwise, it will not work.
Log in the control panel and find the Advanced panel. Click the Cron Jobs icon and type the following details:
For the minutes, remember that it should be */5.
For the command, replace [magento_path] with your root directory in Magento installation URL.
Finally, click Add New Cron Job to insert it in the system.
Running Cron job in the Web Browser
To run cron using your web browser, you need to secure cron.php in your browser first. Then, log in the Magento server. Using bash shell, you can use the syntax below:
To run Magento commands in any directory, add it to your system PATH by using this code: <your Magento install directory>/bin and run them as ./magento <command name>.
Troubleshooting Magento Cron Job
If your cron job is not working, you need to recheck its setup. Make sure that the instructions are valid. If you merely copy commands set in this tutorial, then it won’t work. Adapt the commands according to your server.
Also check your e-mail address. Make sure that you use the correct name in the MAILTO. Why? Because you usually receive a message if something is not working well with the cron task execution.
Finally, keep an eye on output redirection. If you receive a command like this:
Make sure that you address them directly.
As you can see, configuring Magento 2 Cron Jobs is pretty easy. However, you might experience some troubles on the first try. If you do, just double check the commands. If you don’t know what the problem is, don’t hesitate to contact Magento Community, leave a question on Magento forums, or simply ask for help.