Cron job is a great feature by Linux, the free operating system for the user. The cron job will create a command or a script that is appropriate with the task you want to do. Instead of manual working, the cronjob allows running automatically in exact time and date. Due to its automation, the cron jobs is the perfect choice for repeated projects every date or every week. Cron configuration is very important in Magento to set the schedule for many system activities such as reindexing, auto-update of currency rates, Magento emails, etc. Only when the Magento 2 Cron job configuration is correct, the cron job is active. In the case of making an error, this means Magento won’t function as expected.
You may like: Magento 2 hosting: Effective tips to choose the best one
Why need Magento 2 Cron Job
Several Magento features require at least one Magento 2 Cron Job, which schedules activities to occur in the future. A partial list of these activities follows:
- Catalog price rules
- Generating Google sitemaps
- Customer Alerts/Notifications (product price change, product back in stock)
- Private sales (Magento Commerce only)
- Automatic updating of currency rates
- All Magento emails (including order confirmation and transactional)
Run and Create Magento 2 cron job
Please follow the guides to start the cron job program as your wish while the cron schedule is set by you.
- Create a class within the “Cron” folder
- Manually setup the cron schedule by using PHP: bin/magento cron:run
- Find a log in the var/log/system.log after the cronjob has run.
- Login to Magento 2 Admin panel, do as the path: Stores > Configuration > Advanced > System, then changing scheduler settings per cron group.
- Finally, run cron from the command line:
magento cron:run [–group=”<cron group name>”]
Now, We will add a custom cron in HelloWorld module.
Content would be
- group id is your cron group name. You can run only cron for single group at a time.
- job instance is class to be instantiated (classpath).
- job method is method in job instance to call.
- job name is Unique ID for this cron job.
- schedule is schedule in cron format. The following graph shows what it consists of:
In crontab.xml, we have defined job instance is Mageplaza\HelloWorld\Cron\Test. It should create now.
Content would be:
All done, please flush cache and run magento cron:run –group=”default” from the command line.
To check whether the Cron is working properly, go to var/log/cron.log of your store and you will see the text Mageplaza\HelloWorld\Cron\Test::execute in it.
How to determine cron not working?
a) To install Magento 2 Cron Scheduler extension
Nowadays, there are many extensions that perform complex checking and visualization of cron task performance. You can easily find them with the inquiry “Magento 2 cron job scheduler extension” or “Magento 2 cron module”
Magento 2 Cron Scheduler Extension
b) To check directly in the database In case you do not want to install an additional extension, it is enough for you to open up DB (via phpMyAdmin, for example) and run a simple SQL query:
SELECT MAX(executed_at) FROM cron_schedule
If cron works correctly, you get the almost the current time (max 5 min deviation)
SQL Query to cron_schedule table
Another useful SQL query that returns the list of log errors:
SELECT job_code, messages FROM cron_schedule WHERE messages IS NOT NULL
Magento 2 Cron job is not working. What should I do?
In Magento 2 cron job performance is related to the command
Connect via ssh and run the command
crontab -l to obtain the list of active cronjobs. After your running the command
crontab -l > crontab.cfg you will record the current configuration to a file for the following editing. The command
crontab crontab.cfg, vice versa installs the configuration from the file.
Also, you can set up cron via Cpanel, the same configurations, however, via web interface (Cpanel > Cron Jobs or Cpanel > Scheduled Tasks)
If you already have cronjob or you only have to add it , we recommend to use the following command:
* * * * * php -f [store root]/bin/magento cron:run >> [store root]/var/log/magento.cron.log 2>&1;
[store root] is the root directory with installed Magento.
This command is slightly different from a command given in Magento 2 manuals. However, it brings the variety of unquestionable advantages: ANY error and any successful performance will be recorded in a log file.
After new Magento 2 cron job configuring, within the next minute, the log file store
[root]/var/log/magento.cron.log with the error text or successful performance notification “Ran jobs by schedule.” will appear.
Magento 2 Cron Log File
If the file does not occur within the next 1 minute, it means the wrong path to the root directory or no permissions to write to the directory
Magento 2 Cron job is essentially important. That is why you should pay so much attention to it and be sure that it operates correctly.
Level up your website with ArrowHiTech – official partner of Magento
Our own Magento development support services is plus the option for monthly support packages. It is to make sure your online business always remains in top form. And of course, there are countless other developer websites that can be easily found and perused as well, especially push your Magento 2 site speed.
- Headquarter: Vietnam
- Website: https://www.arrowhitech.com/
- Contact: firstname.lastname@example.org
ArrowHiTech Development Agency
Our Magento 2 Products: