Magento 2 run a specific Cron without cron:run CLI command?

On option to save in the development time would be to use N98MageRun for Magento 2.

This has the wonderful commands sys:cron:list and sys:cron:run.

Using these commands you will be able to find the specific job code for your cron and then trigger just that cron from the command line.

It can easily be installed via composer require --dev n98/magerun2 and I would recommend it should be a go to for a dev installation when working with Magento 2


Think simple! The Cron class is a "normal" class. We can use a Playground to test our Cron: How can I bootstrap Magento 2 in a test.php script?. The Object Manager will create our Cron Object. And then, we can test our Cron by calling the url directly on Browser.

** Note if using Nginx you can put these files in the pub/ folder and adjust the require path for the Test.php file to require '../app/bootstrap.php';

Test.php

<?php
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);

TestApp.php

 public function launch()
 {
        /** @var \Vendor\Module\Cron\Test $cron */
        $cron = \Magento\Framework\App\ObjectManager::getInstance()
            ->create('Vendor\Module\Cron\Test');

        $cron->execute();

        return $this->_response;

 }

enter image description here


You can achieve using two files:

create folder and class in root of project like:

crons/CronprocessApp.php

    <?php
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use phpseclib\Net\SFTP;
use phpseclib\Crypt\RSA;

class CronprocessApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface{

    public function __construct(
    \Magento\Framework\App\State $state,\Magento\Framework\App\Response\Http $response)
    {
        $this->_response = $response;
        //$state->setAreaCode('any area'); // or 'adminhtml', depending on your needs
        $state->setAreaCode('adminhtml'); // or 'adminhtml', depending on your needs
    }
    public function launch()
    {
        /** @var \Vendor\Module\Cron\Test $cron */
        $cron = \Magento\Framework\App\ObjectManager::getInstance()
            ->create('Custom\Preorder\Cron\ChangeVisiblityNonPreorderProduct'); //pass the name of your cron class path 
        $cron->execute();       


        return $this->_response;

    }
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
}
?>

Create another class file:

crons/Cronprocess.php

 <?php
require __DIR__ . '/../app/bootstrap.php';
require __DIR__ . '/../crons/cronprocessApp.php';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CronprocessApp');
$bootstrap->run($app);

To Run cron go to cli with project root path and run below command:

php crons/cronprocess.php