CronJob

How to Setup Cron Jobs in Magento 2 via SSH and Cpanel

by admin

Clearly, from the viewpoint of management, the importance of automatic running the routine maintenance jobs in the background is undeniable. Since then, Linux Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going 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 cron jobs in Magento 2. 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. 

How to setup cron jobs in Magento 2?

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

Setup cron jobs via Cpanel

Step 1:  Log in cpanel.

 

Step 2:  Select Cron jobs (Advanced> Cron Jobs).

 

Step 3: Set calendar and command for cron jobs.

cPanel

 

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

 * * * * * <path to php binary> /home/domain/public_html/bin/magento cron:run  | awk ‘{ print strftime(“\%Y-\%m-\%d \%H:\%M:\%S”), $0; fflush(); }’ 2>&l >> ~/magento_cron.log
* * * * * <path to php binary> /home/domain/public_html/update/cron.php | awk ‘{ print strftime(“\%Y-\%m-\%d \%H:\%M:\%S”), $0; fflush(); }’ 2>&l >> ~/magento_cron.log
 * * * * * <path to php binary> /home/domain/public_html/bin/magento setup:cron:run  | awk ‘{ print strftime(“\%Y-\%m-\%d \%H:\%M:\%S”), $0; fflush(); }’ 2>&l >> ~/magento 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 (to check, use: # which php) 
  • home: is the directory where you install the file 

After that, fill in the common settings form, in which:

  •  Command: Import cron command 
Cron jobs common setting

Cron jobs – common settings

 Then click on “Add new cron job”, the result will be the following:

Cron jobs create success message

Setup cron jobs via SSH

By using SSH, access your server’s SSH using SSH software such as Putty or Bitvise.

  • To test the running crones, use the command:  crontab -l

Cron jobs in SSH

  •  To add or edit crons, use the following command: Crontab -e
  •  Go to the last line and enter the following line
*/5 * * * * wget -0 /dev/null -q http://www.DOMAIN.com/MAGENTOPATH/cron.php > /dev/null

Note: Change DOMAIN.com and MAGENTOPATH to the root URL of your Magento installation.

Every 5 minutes, the cron.php file will be executed.

  • Save the cron card with CTRL + X => enter Y to save the changes 

The saving successful message will display like the following line:

Cron jobs are not so complicated as they seem to be. On the other hand, making full use of cronjobs could save you lots of time in managing and controlling the system both internally and externally. 

 

Don’t forget to enrich your knowledge every day with us!

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.