How To Create Cron Job in Magento 2, Configure and Run for eCommerce Store Auto Task Management
Managing an eCommerce store can be overwhelming with numerous urgent tasks. For developers and store owners looking to save time, Magento cron jobs are invaluable. A cron job automates tasks at set intervals, dates, and times, significantly reducing manual effort.
Why Use Cron Jobs in Magento 2?
Cron jobs in Magento 2 automate routine tasks, freeing up time for more critical activities. Common tasks automated by cron jobs include:
- Sending newsletter emails
- Indexing and caching
- Generating sitemaps
- Updating currency rates
By automating these tasks, you can improve your store’s efficiency and overall operations.
Setting Up Cron Jobs in Magento 2
Prerequisites
Ensure your server-level cron module can run Magento cron files. Log in to the Magento 2 server as a user with root privileges to carry out the following steps.
Step 1: Switch to Magento File System Owner
Use the following command to switch to the Magento file system owner:
bash
su
Step 2: Create a Cron Job
Run the following command to open the cron table for the Magento user:
bash
crontab -u
For example, if the username is magento_bh, the command would be:
bash
crontab -u magento_bh -e
Step 3: Add Cron Commands
Add the following commands to the cron table to automate tasks:
bash
php -c
php -c
php -c
Example Commands:
bash
/usr/bin/php -c /etc/php5/apache2/php.ini
/var/www/magento2/bin/magento cron:run >
/var/www/magento2/var/log/magento.cron.log&
/usr/bin/php -c /etc/php5/apache2/php.ini
/var/www/magento2/update/cron.php >
/var/www/magento2/var/log/update.cron.log&
/usr/bin/php -c /etc/php5/apache2/php.ini
/var/www/magento2/bin/magento setup:cron:run >
/var/www/magento2/var/log/setup.cron.log&
Configure Cron Jobs from Magento Admin Panel
- Log in to the Magento 2 Admin panel.
- Navigate to Stores → Settings → Configuration.
- Go to Advanced → System and expand the Cron (Scheduled Tasks) section.
- Configure the options for group
Click Save Config when done.
In conclusion, cron jobs in Magento 2 are essential for automating routine tasks, such as sending newsletters, indexing and caching, generating sitemaps, and updating currency rates. By following these steps, you can efficiently configure and manage cron jobs, ensuring your store runs smoothly and tasks are automated effectively, freeing up time for more critical activities.
Comments