set up magento 2 cronjobs

How to Set up Magento 2 Cronjobs: Comprehensive Tutorial

by admin

In this guide, we’ll walk you through the steps to set up Magento 2 cronjobs, whether you’re using the Admin Panel or the command line.

Clearly, from the viewpoint of management, the importance of automatically running the routine maintenance jobs in the background is undeniable. Since then, Linux Cron utility has been an effective way to schedule a routine background job at a specific time and/or day on an ongoing basis.

On the other hand, a system using Magento could never be implemented comprehensively without several scheduled activities, we may say: catalog price rules, newsletters, generating Google sitemaps, customer alerts/notifications (product price change, product back in stock), reindexing, private sales (Magento Commerce only), automatic updating of currency rates, all Magento e-mails (including order confirmation and transactional.

By and large, this term causes some confusion for the newcomers. Therefore, we will demonstrate the simplest definition of cron jobs and decent ways to successfully set up Magento 2 cronjobs. Let’s have a look!

What is a cron job?

A cron job is a repetitive task, which is executed by the system at a specified time/date. While in Windows, it’s called Task Schedule, in Linux, it is Cron. Generally, cron jobs are very useful for routine tasks like scheduling system scanning, daily backups etc. On the other hand, they can also be helpful in performing tasks that do not modify the data on the server, such as sending out email messages.

A cron job can be set to run within an interval of time, which can be minutes, hours, days in the week, days in months, months or any combination of these elements above.

Time formats

Hereby is the explanation for a time format in Magento cronjobs settings.

Cronjobs - commands to execute

Cron jobs – commands to execute

For more time formats, check out the examples below:

* * * * * [command] Run per minute
0 * * * * [command] Run per hour
0 0 * * * [command] Run at 0:00 am everyday
 0 0 0 * * [command] Run each month
0 0 0 0 * [command] Run each week

How to Set Up Magento 2 Cronjobs?

To create a cron job in Magento 2, firstly you need to log in to ssh then enter the following command crontab -u -e.

For example: crontab -u magento_user -e.

You can see current Cron Jobs by using crontab -l. To fix a Cron Job or add a new one you can use crontab -e, then an editor pops up allowing you to edit the cron job.

Write a Cron Job using command.

Next, to setup cron jobs for Magento, just run the following commands with this order:

* * * * *  <path to php binary> <magento install dir>/bin/magento cron:run | grep -v "Ran jobs by schedule" >> <magento install dir>/var/log/magento.cron.log
* * * * *  <path to php binary> <magento install dir>/update/cron.php >> <magento install dir>/var/log/update.cron.log
* * * * *  <path to php binary> <magento install dir>/bin/magento setup:cron:run >> <magento install dir>/var/log/setup.cron.log

While these above commands are running, the server will run file cron.php at Magento root directory each one minute.

In which:

  • <path to php binary> is the absolute file system path to your PHP binary.
  • <magento install dir> is the directory in which you installed the Magento software, for example: /var/www/html/magento2/

The first command (magento cron:run) reindexes indexers, sends automated e-mails, generates the sitemap, and so on. Usually it’s associated with the PHP command line .ini file. The other two commands are used by the Component Manager and System Upgrade.

For example: you install Magento in /var/www/magento2, to set cron job running every 1 minute, you can follow as below.

*/1 * * * * php /var/www/html/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log
*/1 * * * * php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
*/1 * * * * php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log

To save it after all modifications, click Esc then use :wq and enter. In case you don’t want to save any changes as above you can use :q enter.

How to configure cron job in Magento 2 via Admin Panel?

On the Magento Admin, follow the path of selection Stores > Configuration > Advanced > System > Cron (Scheduled Tasks).

magento 2 cron configuration admin

Expand the Cron configuration options for group: Index and Cron configuration options for group: Default sections and complete all information as shown below:

Cron configuration options for group - Index

Cron configuration options for group - default

Press Save Config.

Secure cron.php to run in a browser

You have two ways of securing Magento 2 cron: using Apache or Nginx.

Secure cron with Apache

Create a password file

First, you need to create a password file. For security purposes, you shouldn’t locate your file under your web server docroot. Below we stored cron in a new directory as an example:

Enter the following commands as a user with root privileges:

$ mkdir -p /usr/local/apache/password
$ htpasswd -c /usr/local/apache/password/passwords <username>

The <username> can be the web server or another user.

Follow the prompt on your screen to create a password for the user.

In order to add another user to your password file, write the following command with root privileges:

$ htpasswd /usr/local/apache/password/passwords <username>

After creating a password file, you can choose to add a user to create an authorized group, but this step is optional. The instruction for the step can be found here.

Secure cron in .htaccess

To secure cron in .htaccess, you should follow these steps:

  1. Log in to your Magento server as, or switch to, the Magento file system owner.
  2. Open <magento_root>/pub/.htaccess in a text editor.

(Because cron.php location is in the pub directory, edit .htaccess only.)

  1. Case 1: Cron access for one or more users. Replace the existing <Files cron.php> directive with the following:
<Files cron.php>

   AuthType Basic

   AuthName "Cron Authentication"

   AuthUserFile /usr/local/apache/password/passwords

   Require valid-user

</Files>
  1. Case 2: Cron access for a group. Replace the existing <Files cron.php> directive with the following:
<Files cron.php>

   AuthType Basic

   AuthName "Cron Authentication"

   AuthUserFile /usr/local/apache/password/passwords

   AuthGroupFile <path to optional group file>

   Require group <name>

</Files>
  1. Save the .htaccess edits.
  2. Verify if cron is secure.

Secure cron with Nginx

Like with Apache, you also need to create a password file and modify the Nginx configuration to reference the password file when accessing the pub/cron.php. Read the following sources to know how to set up a password file.

Secure cron in nginx.conf.sample

Magento gives an optimized sample Nginx configuration file out of the box. You can modify it to secure cron.

Add the part below to your Magento nginx.sample.conf file:

#Securing cron

location ~ cron\.php$ {

   auth_basic "Cron Authentication";

   auth_basic_user_file /etc/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;

   include      fastcgi_params;

}

After saving the file changes, you can restart Nginx using this command:

$  systemctl restart nginx

Finally, Verify if cron is secure.

Disable cron jobs

In certain situations, you might want to manually disable cron job before you complete the maintenance task, as well as cache management, in order to prevent performance issues. Use the ece-tools CLI command cron:disable to disable all Magento cron jobs and stop any active cron processes.

The steps to disable cron jobs:

  1. Use SSH to log in to your environment.
  2. Disable cron jobs and stop all active cron processes. The command is as stated below:
./vendor/bin/ece-tools cron:disable
  1. Once you completed the maintenance task, make sure to enable the cron jobs again:
./vendor/bin/ece-tools cron:enable

Conclusion

Setting up cron jobs in Magento 2 is essential for automating key tasks and maintaining your store’s efficiency. By following this guide, you can easily configure cron jobs to streamline operations like indexing, email updates, and cache management. If you encounter any issues, feel free to share your questions or explore our Extensions for Magento 2 for more optimization solutions.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.