How Can You Schedule Custom Cron In Magento2?

CronJob is one of the most important Magento 2 features.It helps you schedule and run certain tasks on your server. Cron jobs are used to automating tasks that run in the background periodically.

Cron Folder Structure:

Following steps to create a custom cron job extension in Magento 2:

Step 1: Create an etc folder and add the Module.xml file .
Step 2: Create a registration.php file
Step 3: Create etc/crontab.xml
  • Group id is the cron group name
  • Job instance is a class to be instantiated.
  • A schedule is defined using the unix-cron string format ( * * * * * ) which is a set of five fields in a line, indicating when the job should be executed.
Step 4: Create HelloWorld.php

File path:app/code/Aspl/CronJob/Cron/HelloWorld.php

To run cron manually:

Run Manually  – Type in the command line in the root foot folder if magento2

  • php bin/magento cron:run

Run Automatically – Install cron with below commands:

  • sudo php bin/magento cron:install
  • crontab -l

Leave a Reply

Your email address will not be published.