Cron is Linux utility which schedules a command or script on server to be run automatically at a specified time and date. Cron jobs are very useful to automate recurring tasks.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Magento queries.
In this context, we shall look into how to setup Cron Job in Magento.
One of the most distinguishing features of Magento 2 is cron job.
Several Magento features require at least one cron job, which schedules activities to occur in the future.
The cron job will create a command or a script that is appropriate with the task you want to do.
Generally, Magento2 required at least one cron to schedule important activities.
Some of those activities are:
The Magento crontab is the configuration used to run Magento cron jobs.
Magento uses cron tasks that can run with different configurations.
The PHP command-line configuration controls the general cron job that reindexes indexers, generates e-mails, generates the sitemap, and so on.
Starting with version 2.2, Commerce creates a crontab for you.
We add the Commerce crontab to any configured crontab for the Commerce file system owner.
The Commerce crontab is inside #~ MAGENTO START and #~ MAGENTO END comments in your crontab.
1. Firstly, log in as, or switch to, the Magento file system owner.
2. Then, change to your Magento installation directory.
3. Finally, enter the following command:
bin/magento cron:install [–force]
Use --force to rewrite an existing Magento crontab.
Note:
To view the crontab, enter the following command as the Magento file system owner:
crontab -l
Make sure you run cron as a user with permission to write to the Magento file system.
The following syntax is used to run cron from the command line
Command options:
bin/magento cron:run [--group=""]
where --group specifies the cron group to run (omit this option to run cron for all groups)
To run the indexing cron job, enter:
bin/magento cron:run --group index
To run the default cron job, enter:
bin/magento cron:run --group default
This article covers how to run cron job manually in Magento 2. Basically, Cron Job is one of the important features of Magento.
Generally, the syntax to write a cron is:
* * * * * /etc/cron.daily/script.sh
Here,
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute
/etc/cron.daily/script.sh – file to be executed.
This job will run every minute.
To run cron manually:
1. To run all crons:
$ php bin/magento cron:run
2. To run crons added under a group:
$ php bin/magento cron:run --group="your_group_id"
In our case, group id is default.
So, command will be:
$ php bin/magento cron:run --group="default"