{"id":442,"date":"2019-05-29T19:43:23","date_gmt":"2019-05-29T19:43:23","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/05\/29\/set-up-custom-cron-job-in-magento-2\/"},"modified":"2025-05-22T16:48:24","modified_gmt":"2025-05-22T11:18:24","slug":"set-up-custom-cron-job-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/set-up-custom-cron-job-in-magento-2\/","title":{"rendered":"How to Set up Custom Cron Job in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Cron job enables to run a set of commands at scheduled date and time repeatedly. Fortunately, Magento 2 store owners can use this functionality in the store! However, before making any changes to your live site, it\u2019s essential to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/set-up-staging-environment-in-magento-2\/\">set up a staging environment in Magento 2<\/a>. This allows you to test any updates or changes without risking disruption to your live site.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The method to set up custom cron job in Magento 2 helps the store owner to effectively manage tasks like keeping the caches and indexes up to date and timely cleanups. Moreover, there are tasks that need to be done later in order to avoid system overload, or simply due to the requirement of condition fulfillment. But if want some of the tasks to be done in decided hours you need to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/dynamically-schedule-cron-job-in-magento-2-system-configuration\/\">Dynamically Schedule Cron Job in Magento 2 System Configuration<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the customers who bought your product are expected to leave a review, and a reminder Email is supposed to be sent after a few days of the purchase. The review reminder Email can be automated in the Magento 2 store using the below method!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are other tasks for which a custom cron job can be helpful such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Generating Google sitemaps<\/li>\n\n\n\n<li>Customer alerts or notifications regarding product price change, product back in stock, etc.<\/li>\n\n\n\n<li>All Magento e-mails (including order confirmation and transactional)<\/li>\n\n\n\n<li>Catalog price rules<\/li>\n\n\n\n<li>Newsletters<\/li>\n\n\n\n<li>Reindexing<\/li>\n\n\n\n<li>Private sales (Magento Commerce only)<\/li>\n\n\n\n<li>Automatic updating of currency rates<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Set up Custom Cron Job in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file in&nbsp;<em><strong>app\\code\\[Vendor]\\[Namespace]\\<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n        \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n            \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n            '[Vendor]_[Namespace]',\n            __DIR__\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file in&nbsp;<em><strong>app\\code[Vendor][Namespace]\\etc<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n        &lt;module name=\"[Vendor]_[Namespace]\" setup_version=\"1.0.0\"\/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>crontab.xml<\/strong>&nbsp;file in&nbsp;<em><strong>app\\code[Vendor][Namespace]\\etc<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Cron:etc\/crontab.xsd\">\n        &lt;group id=\"default\">\n            &lt;job instance=\"[Vendor]\\[Namespace]\\Cron\\Reminder\" method=\"execute\" name=\"vendor_namespace_cron\">\n                &lt;schedule>0 4 * * *&lt;\/schedule> &lt;!-- here you need to schedule time to run cron job -->\n            &lt;\/job>\n        &lt;\/group>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>Reminder.php<\/strong>&nbsp;file in&nbsp;<em><em><strong>app\\code[Vendor][Namespace]\\Cron<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n    namespace [Vendor]\\[Namespace]\\Cron;\n\n\n    class Reminder\n    {\n        \n        public function execute()\n        {\n\n            \/*\n            *\n            * Here you need to define logic\n            *\n            *\/\n            return $this;\n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the above method, automate the repetitive tasks and optimize the store management system!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Any doubts in the implementation are welcome and I\u2019d be happy to help  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cron job enables to run a set of commands at scheduled date and time repeatedly. Fortunately, Magento 2 store owners can use this functionality in&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-442","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":15402,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions\/15402"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}