Answers.Body: <p>The Craft CLI can be invoked from CRON just like any other binary on your system! Keep in mind that the command will be run by whatever user triggers the task—so that may not be the same user as the HTTP or PHP-FPM processes run as.</p>
  <pre><code>*/30 * * * * /usr/bin/env php /path/to/craft custom-command/cron-scheduler
  </code></pre>
  <p>Check out <a href="https://crontab.guru" rel="nofollow noreferrer">Crontab Guru</a> if the CRON syntax gives you any trouble. We also have a bit about invoking CLI commands with CRON in the <a href="https://craftcms.com/docs/5.x/system/queue.html#cron" rel="nofollow noreferrer">Queue Runners</a> section of the documentation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-11T14:52:03.647Z
Answers.LastActivityDate: 2024-06-11T14:52:03.647Z
Answers.UserDisplayName: August Miller
Body: <p>I created a custom command to print logs.
  Now I want to schedule that command to execute every hour.</p>
  <p>Can any one give me a step-by-step process to achieve that? I am new to craft CMS.</p>
  <p>Craft CMS version: 3.9</p>
  <p>Here is my command file:</p>
  <p>File path: <strong>modules/customcommand/src/console/controllers/CronSchedulerController.php</strong></p>
  <pre><code>&lt;?php
  
  namespace modules\customcommand\console\controllers;
  
  use Craft;
  use craft\console\Controller;
  
  class CronSchedulerController extends Controller
  {
      public $defaultAction = 'handler';
  
      public function actionHandler(): int
      {
          Craft::info('Cron Schedule custom executed', 'cron_scheduler_command');
          // my other stuff...
          return 0;
      }
  }
  </code></pre>
  <p>Executing commands using the command line is working properly:</p>
  <pre><code>php craft custom-command/cron-scheduler
  </code></pre>
  
Tags: <cron><commands>
Title: How to schedule custom command that executes every hour
ThreadText: # How to schedule custom command that executes every hour
  
  (Asked by: username_0 on 2024-06-11)
  
  I created a custom command to print logs.
  Now I want to schedule that command to execute every hour.
  
  Can any one give me a step-by-step process to achieve that? I am new to craft CMS.
  
  Craft CMS version: 3.9
  
  Here is my command file:
  
  File path: **modules/customcommand/src/console/controllers/CronSchedulerController.php**
  
  ```
  <?php
  
  namespace modules\customcommand\console\controllers;
  
  use Craft;
  use craft\console\Controller;
  
  class CronSchedulerController extends Controller
  {
      public $defaultAction = 'handler';
  
      public function actionHandler(): int
      {
          Craft::info('Cron Schedule custom executed', 'cron_scheduler_command');
          // my other stuff...
          return 0;
      }
  }
  
  ```
  
  Executing commands using the command line is working properly:
  
  ```
  php craft custom-command/cron-scheduler
  
  ```
  
  **Tags:** cron, commands
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-06-11. Score: 2
  
  The Craft CLI can be invoked from CRON just like any other binary on your system! Keep in mind that the command will be run by whatever user triggers the task—so that may not be the same user as the HTTP or PHP-FPM processes run as.
  
  ```
  */30 * * * * /usr/bin/env php /path/to/craft custom-command/cron-scheduler
  
  ```
  
  Check out [Crontab Guru](https://crontab.guru) if the CRON syntax gives you any trouble. We also have a bit about invoking CLI commands with CRON in the [Queue Runners](https://craftcms.com/docs/5.x/system/queue.html#cron) section of the documentation.
  
  
  ---
  
  