Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

in this tuto, I'll show you how to run cron programmatically in Drupal 8.

Drupal core has an Interface for running cron tasks Drupal\Core\CronInterface.

To Execute a cron run, juste apply Drupal\Core\CronInterface::run() method.


namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\CronInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

 * Class MyModuleController
 * @package Drupal\mymodule\Controller
class MyModuleController extends ControllerBase
   * @var CronInterface
  protected $cron;

   * MyModuleController constructor.
   * @param CronInterface $cron
  public function __construct(CronInterface $cron){
    $this->cron = $cron;

   * @param ContainerInterface $container
   * @return ControllerBase|MyModuleController
  public static function create(ContainerInterface $container) {
    return new static(

   * @return array
  public function index()
    $this->messenger()->addMessage($this->t('Cron ran successfully.'));
    return array(
      '#type' => 'markup',
      '#markup' => "Cron ran successfully."






Add new comment

Restricted HTML


Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.


Contact Us

All the content is FREE but I still need your help


Become a patreon