The Magento cron module is without doubt one of the most vital options in Magento 2 as a result of it automates duties like:
Right here, we’ll clarify all the pieces you want to find out about Magento cron, together with what it’s, the right way to set up it, and the steps to safe it.
Right here’s what we’ll cowl:
Magento makes use of cron, a time-based process scheduling utility for Unix techniques, to carry out repetitive duties corresponding to updating catalog pricing and stock on a schedule. With out the Magento 2 cron module, you’ll must execute these duties manually.
Magento cron depends on a configuration file referred to as the crontab, quick for cron desk, that specifies shell instructions and scripts and their execution schedule. Right here’s the default crontab file in Magento 2.4.
One crontab can include a number of duties, or cron jobs, associated to a particular person or software.
Every cron job is listed as a time string consisting of 5 variables that outline the frequency of every process, separated by a clean house and adopted by the command to be executed. The next desk describes the appropriate values for every variable.
Variable Place | Descriptor | Acceptable Values |
1 | Minute | 0 to 59, or * (no particular worth) |
2 | Hour | 0 to 23, or * (no particular worth) in UTC time. |
3 | Day of the month | 1 to 31, or * (no particular worth) |
4 | Month | 1 to 12, or * (no particular worth) |
5 | Day of the week | 0 to 7 (each 0 and seven characterize Sunday), or * (no particular worth) |
All cron jobs contained in the crontab should use an appropriate worth as listed above. With out it, the crontab received’t run. The one exception is a ahead slash (/) that you should use to execute a process at an everyday interval, no matter the time.
For instance, to run a command each Wednesday at 6 pm, you may enter the cron schedule as 0 6 * * 3. And if you wish to run the duty each six hours, you may set the cron schedule as * */6 * * *.
Magento 2 Open Supply categorizes cron jobs into three teams:
Nonetheless, different Magento 2 extensions could add cron jobs and teams to your retailer. You possibly can view the checklist of cron jobs in your Magento retailer by logging into the Magento database and getting into the next command:
$ choose job_code from cron_schedule GROUP BY job_code;
Magento logs all cron job info in a devoted magento.cron.log file within the var/log/ folder and in addition writes it to the cron_schedule desk within the Magento database with info corresponding to:
You possibly can view the data within the Magento database utilizing the next command:
$ SELECT * from cron_schedule;
That command will output all rows from the Magento cron_schedule database desk. As an alternative, you should use the next command to output the 30 most up-to-date rows.
$ SELECT * from cron_schedule ORDER BY schedule_id DESC LIMIT 30;
Now that you just perceive what Magento cron is, let’s discover the steps to put in it.
As of Magento model 2.2, you may set up Magento 2 cron utilizing the cron:set up command. Right here’s how you are able to do it from the Magento command line interface (CLI).
Log in to your Magento server because the filesystem proprietor and run the next command from the Magento root listing:
$ bin/magento cron:set up
Confirm the crontab utilizing the next command:
$ crontab -l
You’ll see an output just like:
That’s it. You’ve efficiently put in Magento 2 cron.
Log in to your Magento server over SSH, navigate to the venture root, and run the next command because the Magento filesystem proprietor to run cron jobs manually:
$ bin/magento cron:run
Ensure you run the command no less than two occasions: first to find cron duties after which to run them.
Optionally, you may also run Magento cron for a particular group utilizing:
$ bin/magento cron:run --group <group-name>
Substitute the <group-name> placeholder with a cron group identify corresponding to index or default to run the cron duties for these teams.
Magento restricts entry to the cron.php file by default. Though you may override it by enhancing the .htaccess file within the pub/ folder, we don’t suggest doing it on a manufacturing retailer as it might depart your retailer weak to exploits.
Nonetheless, in the event you’re working in a improvement surroundings, operating cron out of your browser might help you pace up improvement.
Observe these steps to safe the Magento cron.php file earlier than you entry it from an online browser.
Word: Until specified in any other case, it’s essential to execute all instructions on this part as a person with root privileges.
The steps to arrange HTTP authentication will fluctuate primarily based in your net browser. Observe these steps to arrange HTTP authentication with Apache:
Create a brand new listing to retailer your password file utilizing:
$ mkdir -p /usr/native/apache/password
Then create a password file inside that listing utilizing:
$ htpasswd -c /usr/native/apache/password/passwords <username>
Observe the prompts within the terminal to set a password for the person. When you’re executed, proceed to the subsequent step.
The steps to replace your net server configuration file fluctuate primarily based in your net server software.
To safe cron for Apache, swap to the Magento filesystem person and navigate to the Magento root. Edit the .htaccess file contained in the pub/ folder utilizing your most well-liked textual content editor and change the default <Information cron.php> directive with the next textual content:
<Information cron.php>
AuthType Primary
AuthName "Cron Authentication"
AuthUserFile /usr/native/apache/password/passwords
Require valid-user
</Information>
Save the .htaccess file and proceed to the subsequent step.
To safe cron for Nginx, swap to the Magento filesystem person and navigate to the Magento root. Edit the default nginx.conf.pattern file within the venture root and add the next textual content to it:
#Securing cron
location ~ cron.php$ {
auth_basic "Cron Authentication";
auth_basic_user_file /and many others/nginx/.htpasswd;
try_files $uri =404;
fastcgi_pass fastcgi_backend;
fastcgi_buffers 1024 4k;
fastcgi_read_timeout 600s;
fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
embody fastcgi_params;
}
Save the file, swap again to the basis person, and restart Nginx utilizing:
$ sudo systemctl restart nginx
After that, proceed to the subsequent step.
The best technique to confirm the cron.php file is safe is by accessing it in an online browser. You are able to do that by opening your Magento retailer house web page and including cron.php to the top of the URL.
For instance, https://www.instance.com/cron.php.
You need to see the next immediate in your browser:
Adobe helps you to to configure cron schedule for Magento 2 in two methods:
Right here’s a fast overview of each strategies.
Log in to your Magento server because the filesystem proprietor and navigate to the Magento venture root.
View the present crontab schedule utilizing the next command:
$ crontab -l
The default Magento cron interval is about to * * * * *. You possibly can edit this worth and set a customized schedule to run cron much less ceaselessly. As an example, you may set it to run each 5 minutes by changing the default entry with */5 * * * *.
Use an internet cron expression editor like crontab guru or Cronhub to create a customized cron job for Magento 2 and edit the crontab utilizing the next command:
$ crontab -e
If that is your first time utilizing the crontab edit command, you’ll see the next immediate in your display screen:
Press Enter to proceed with the default textual content editor — nano — or select your most well-liked editor by getting into its corresponding quantity.
Navigate your cursor to the Magento cron job configuration contained in the #~ MAGENTO START and #~ MAGENTO END feedback. Substitute the default values along with your customized expression. Then, save the file and exit the textual content editor.
That’s it. Magento cron will now run on the newly configured schedule contained in the crontab.
Log in to the Magento admin panel and navigate to Shops > Settings > Configuration. Increase the Superior setting choice within the left panel and select System.
Increase the Cron (Scheduled Duties) part and replace the values for every group by clicking the Use system worth checkbox subsequent to every setting.
When you’re arrange cron jobs to run in your most well-liked schedule, click on Save Config.
Magento cron is without doubt one of the most helpful options in Magento 2. It automates sophisticated duties so you may deal with rising your on-line enterprise — identical to Managed Magento Internet hosting with Nexcess.
Our internet hosting plans provide you with entry to Magento-optimized servers with out coping with the technicalities of server safety and upkeep.
Why host with Nexcess? Browse our plans to be taught extra.