|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +group: config-guide |
| 4 | +subgroup: 12_cron |
| 5 | +title: Configure a custom cron job and cron group (tutorial) |
| 6 | +menu_title: Configure a custom cron job and cron group (tutorial) |
| 7 | +menu_order: 3 |
| 8 | +menu_node: |
| 9 | +version: 2.0 |
| 10 | +github_link: config-guide/cron/custom-cron-tut.md |
| 11 | +--- |
| 12 | + |
| 13 | +This tutorial shows you step-by-step how to create a custom cron job and optionally a cron group in a sample {% glossarytooltip c1e4242b-1f1a-44c3-9d72-1d5b1435e142 %}module{% endglossarytooltip %}. You can use a module you already have or you can use a sample module from our [`magento2-samples` repository](https://github.com/magento/magento2-samples){:target="_blank"}. |
| 14 | + |
| 15 | +Running the cron job results in a row being added to the `cron_schedule` table with the name of the cron job, `custom_cron`. |
| 16 | + |
| 17 | +We also show you how to optionally create a cron group, which you can use to run custom cron jobs with settings other than Magento application defaults. |
| 18 | + |
| 19 | +In this tutorial, we assume the following: |
| 20 | + |
| 21 | +* The Magento application is installed in `/var/www/html/magento2` |
| 22 | +* Your Magento database user name and password are both `magento` |
| 23 | +* You perform all actions as the [Magento file system owner]({{ page.baseurl }}install-gde/prereq/file-sys-perms-over.html) |
| 24 | + |
| 25 | +## Step 1: Get a sample module {#cron-tut-get} |
| 26 | +To set up a custom cron job, you need a sample module. We suggest the `magento-module-minimal` module. |
| 27 | + |
| 28 | +If you already have a sample module, you can use it; skip this step and the next step and continue with [Step 3: Create a class to run cron](#cron-tut-class). |
| 29 | + |
| 30 | +{% collapsible To get a sample module: %} |
| 31 | + |
| 32 | +1. Log in to your Magento server as, or switch to, the [Magento file system owner]({{ page.baseurl }}install-gde/prereq/file-sys-perms-over.html). |
| 33 | +2. Change to a directory that is not in your Magento application root (for example, your home directory). |
| 34 | +2. Clone the [`magento2-samples` repository](https://github.com/magento/magento2-samples){:target="_blank"}. |
| 35 | + |
| 36 | + For example, |
| 37 | + |
| 38 | + cd ~ |
| 39 | + git clone [email protected]:magento/magento2-samples.git |
| 40 | + |
| 41 | + If the command fails with the error `Permission denied (publickey).`, you must [add your SSH public key to github.com](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account){:target="_blank"}. |
| 42 | +4. Make a directory to which to copy the sample code: |
| 43 | + |
| 44 | + mkdir -p /var/www/html/magento2/app/code/Magento/SampleMinimal |
| 45 | +5. Copy the sample module code: |
| 46 | + |
| 47 | + cp -r ~/magento2-samples/sample-module-minimal/* /var/www/html/magento2/app/code/Magento/SampleMinimal |
| 48 | +5. Verify the files copied properly: |
| 49 | + |
| 50 | + ls -al /var/www/html/magento2/app/code/Magento/SampleMinimal |
| 51 | + |
| 52 | + You should see the following result: |
| 53 | + |
| 54 | + drwxrwsr-x. 4 magento_user apache 4096 Oct 30 13:19 . |
| 55 | + drwxrwsr-x. 121 magento_user apache 4096 Oct 30 13:19 .. |
| 56 | + -rw-rw-r--. 1 magento_user apache 372 Oct 30 13:19 composer.json |
| 57 | + drwxrwsr-x. 2 magento_user apache 4096 Oct 30 13:19 etc |
| 58 | + -rw-rw-r--. 1 magento_user apache 10376 Oct 30 13:19 LICENSE_AFL.txt |
| 59 | + -rw-rw-r--. 1 magento_user apache 10364 Oct 30 13:19 LICENSE.txt |
| 60 | + -rw-rw-r--. 1 magento_user apache 1157 Oct 30 13:19 README.md |
| 61 | + -rw-rw-r--. 1 magento_user apache 270 Oct 30 13:19 registration.php |
| 62 | + drwxrwsr-x. 3 magento_user apache 4096 Oct 30 13:19 Test |
| 63 | +6. Update the Magento database and schema: |
| 64 | + |
| 65 | + php /var/www/html/magento2/bin/magento setup:upgrade |
| 66 | + |
| 67 | +{% endcollapsible %} |
| 68 | + |
| 69 | +## Step 2: Verify the sample module {#cron-tut-verify} |
| 70 | +Before you continue, make sure the sample module is registered and enabled. |
| 71 | + |
| 72 | +{% collapsible To verify the sample module: %} |
| 73 | + |
| 74 | +1. Log in to the Magento Admin as an administrator. |
| 75 | +2. Click **Stores** > **Configuration** > ADVANCED > **Advanced**. |
| 76 | +3. In the right pane, under Disable Modules Output, look for **Magento_SampleMinimal** as the following figure shows. |
| 77 | + |
| 78 | + {:width="900px"} |
| 79 | + |
| 80 | +If the module doesn't display, review [step 1](#cron-tut-get) carefully. Make sure your code is in the correct directory. Spelling and case are important; if anything is different, the module won't load. Also, don't forget to run `magento setup:upgrade`. |
| 81 | + |
| 82 | +{% endcollapsible %} |
| 83 | + |
| 84 | +## Step 3: Create a class to run cron {#cron-tut-class} |
| 85 | +This step shows a simple class to create a cron job. The class only writes a row to the `cron_schedule` table that confirms it's set up successfully. |
| 86 | + |
| 87 | +{% collapsible To create a class: %} |
| 88 | + |
| 89 | +1. Create a directory for the class and change to that directory: |
| 90 | + |
| 91 | + mkdir /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron && cd /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron |
| 92 | +2. Created a file named `Test.php` in that directory with the following contents: |
| 93 | + |
| 94 | +{% highlight php %} |
| 95 | +<?php |
| 96 | +namespace Magento\SampleMinimal\Cron; |
| 97 | +use \Psr\Log\LoggerInterface; |
| 98 | +
|
| 99 | +class Test { |
| 100 | + protected $logger; |
| 101 | +
|
| 102 | + public function __construct(LoggerInterface $logger) { |
| 103 | + $this->logger = $logger; |
| 104 | + } |
| 105 | + |
| 106 | +/** |
| 107 | + * Write to system.log |
| 108 | + * |
| 109 | + * @return void |
| 110 | + */ |
| 111 | + |
| 112 | + public function execute() { |
| 113 | + $this->logger->info('Cron Works'); |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | +{% endhighlight %} |
| 118 | + |
| 119 | +<!-- ?> --> |
| 120 | + |
| 121 | +{% endcollapsible %} |
| 122 | + |
| 123 | +## Step 4: Create `crontab.xml` {#cron-tut-crontab} |
| 124 | +`crontab.xml` sets a schedule to run your custom cron code. |
| 125 | + |
| 126 | +{% collapsible To create crontab.xml: %} |
| 127 | + |
| 128 | +Create `crontab.xml` as follows in the `/var/www/html/magento2/app/code/Magento/SampleMinimal/etc` directory: |
| 129 | + |
| 130 | +{% highlight xml %} |
| 131 | +<?xml version="1.0"?> |
| 132 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> |
| 133 | + <group id="default"> |
| 134 | + <job name="custom_cronjob" instance="Magento\SampleMinimal\Cron\Test" method="execute"> |
| 135 | + <schedule>* * * * *</schedule> |
| 136 | + </job> |
| 137 | + </group> |
| 138 | +</config> |
| 139 | +{% endhighlight %} |
| 140 | + |
| 141 | +The preceding `crontab.xml` runs the `Magento/SampleMinimal/Cron/Test.php` class once per minute, resulting in a row being added to the `cron_schedule` table. |
| 142 | + |
| 143 | +{% endcollapsible %} |
| 144 | + |
| 145 | +## Step 5: Verify the cron job {#cron-tut-cronver} |
| 146 | +This step shows how to verify the custom cron job successfully using a SQL query on the `cron_schedule` database table. |
| 147 | + |
| 148 | +{% collapsible To verify cron: %} |
| 149 | + |
| 150 | +1. Run Magento cron jobs: |
| 151 | + |
| 152 | + php /var/www/html/magento2/bin/magento cron:run |
| 153 | +2. Enter the `magento cron:run` command two or three times. |
| 154 | + |
| 155 | + The first time you enter the command, it queues jobs; subsequently, the cron jobs are run. You must enter the command _at least_ twice. |
| 156 | +2. Run the SQL query `SELECT * from cron_schedule WHERE job_code like '%custom%'` as follows: |
| 157 | + |
| 158 | + 1. Enter `mysql -u magento -p` |
| 159 | + 2. At the `mysql>` prompt, enter `use magento;` |
| 160 | + 3. Enter `SELECT * from cron_schedule WHERE job_code like '%custom%';` |
| 161 | + |
| 162 | + The result should be similar to the following: |
| 163 | + |
| 164 | + +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+ |
| 165 | + | schedule_id | job_code | status | messages | created_at | scheduled_at | executed_at | finished_at | |
| 166 | + +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+ |
| 167 | + | 3670 | custom_cronjob | success | NULL | 2016-11-02 09:38:03 | 2016-11-02 09:38:00 | 2016-11-02 09:39:03 | 2016-11-02 09:39:03 | |
| 168 | + | 3715 | custom_cronjob | success | NULL | 2016-11-02 09:53:03 | 2016-11-02 09:53:00 | 2016-11-02 09:54:04 | 2016-11-02 09:54:04 | |
| 169 | + | 3758 | custom_cronjob | success | NULL | 2016-11-02 10:09:03 | 2016-11-02 10:09:00 | 2016-11-02 10:10:03 | 2016-11-02 10:10:03 | |
| 170 | + | 3797 | custom_cronjob | success | NULL | 2016-11-02 10:24:03 | 2016-11-02 10:24:00 | 2016-11-02 10:25:03 | 2016-11-02 10:25:03 | |
| 171 | + +-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+ |
| 172 | + |
| 173 | +3. (Optional) Verify messages are written to Magento's system log: |
| 174 | + |
| 175 | + cat /var/www/html/magento2/var/log/system.log |
| 176 | + |
| 177 | + You should see one or more entries like the following: |
| 178 | + |
| 179 | + [2016-11-02 22:17:03] main.INFO: Cron Works [] [] |
| 180 | + |
| 181 | + These messages come from the `execute` method in `Test.php`: |
| 182 | + |
| 183 | + public function execute() { |
| 184 | + $this->logger->info('Cron Works'); |
| 185 | + |
| 186 | +If the SQL command and system log contain no entries, run the `magento cron:run` command a few more times and wait. It can take some time for the database to update. |
| 187 | + |
| 188 | +{% endcollapsible %} |
| 189 | + |
| 190 | +## Step 6 (optional): Set up a custom cron group |
| 191 | +This step shows how to optionally set up a custom cron group. You should set up a custom cron group you want your custom cron job to run on a different schedule than other cron jobs (typically, once per minute) or if you want several custom cron jobs to run with different settings. |
| 192 | + |
| 193 | +{% collapsible To set up a custom cron group: %} |
| 194 | + |
| 195 | +1. Open `crontab.xml` in a text editor. |
| 196 | +2. Change `<group id="default">` to `<group id="custom_crongroup">` |
| 197 | +3. Exit the text editor. |
| 198 | +4. Create `/var/www/html/magento2/app/code/Magento/SampleMinimal/etc/cron_groups.xml` with the following contents: |
| 199 | + |
| 200 | +{% highlight xml %} |
| 201 | +<?xml version="1.0"?> |
| 202 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd"> |
| 203 | + <group id="custom_crongroup"> |
| 204 | + <schedule_generate_every>1</schedule_generate_every> |
| 205 | + <schedule_ahead_for>4</schedule_ahead_for> |
| 206 | + <schedule_lifetime>2</schedule_lifetime> |
| 207 | + <history_cleanup_every>10</history_cleanup_every> |
| 208 | + <history_success_lifetime>60</history_success_lifetime> |
| 209 | + <history_failure_lifetime>600</history_failure_lifetime> |
| 210 | + </group> |
| 211 | +</config> |
| 212 | +{% endhighlight %} |
| 213 | + |
| 214 | +For a description of what the options mean, see [Configure custom cron jobs and cron groups reference]({{ page.baseurl }}config-guide/cron/custom-cron-ref.html). |
| 215 | + |
| 216 | +{% endcollapsible %} |
| 217 | + |
| 218 | +## Step 7 (optional): Verify your custom cron group |
| 219 | +This step shows how to verify your custom cron group using the {% glossarytooltip 18b930cf-09cc-47c9-a5e5-905f86c43f81 %}Magento Admin{% endglossarytooltip %}. |
| 220 | + |
| 221 | +{% collapsible To verify your custom cron group: %} |
| 222 | + |
| 223 | +1. Run Magento cron jobs for your custom group: |
| 224 | + |
| 225 | + php /var/www/html/magento2/bin/magento cron:run --group="custom_crongroup" |
| 226 | + |
| 227 | + Run the command at least twice. |
| 228 | +2. Clean the Magento cache: |
| 229 | + |
| 230 | + php /var/www/html/magento2/bin/magento cache:clean |
| 231 | +2. Log in to the Magento Admin as an administrator. |
| 232 | +3. Click **Stores** > **Configuration** > **Advanced** > **System**. |
| 233 | +4. In the right pane, expand **Cron**. |
| 234 | + |
| 235 | + Your cron group displays as follows: |
| 236 | + |
| 237 | +  |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | +{% endcollapsible %} |
| 242 | + |
0 commit comments