0

In the Magento 2 CLI, the command to run cron jobs is cron:run --group="[cron group]"

However, I was not able to find how this translates into actual PHP code, i.e. where cron:run is mapped to.

Is it possible to execute cron:run programmatically from within my module? Something like:

function run() {
    Cron::execute(CRON_GROUP);
}
  • Can you explain a bit more on why you would need to do that? Thanks – Dominic Xigen Jul 10 at 14:55
  • Actually it's for testing purposes--I have a module that executes a number of behaviors in the main Magento 2 module and provides a REST interface to control this. I could imagine some other use cases though, i.e. if someone wanted to run all crons at a certain frequency regardless of the actual frequency defined by the module developer, etc. – Ynhockey Jul 10 at 14:59
  • what have you tried to achieve this? – Philipp Sander Jul 10 at 15:00
  • I tried checking what Magento itself run on cron:run, but beyond that I really don't have any direction. – Ynhockey Jul 10 at 15:06
1

Inside here

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Cron/Console/Command/CronCommand.php#L116

$params[self::INPUT_KEY_GROUP] = $input->getOption(self::INPUT_KEY_GROUP);

$cronObserver = $objectManager->create(\Magento\Framework\App\Cron::class, ['parameters' => $params]);

So basically you are passing $params['group'] = whatever

And then

$cronObserver->launch();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.