2

I have created a cronjob in:

app/code/Vendor/Module/Cron/MyCron.php

It's in development phase, so every time I need to check the data or debug anything I am creating a log file and rescheduling it to run cron.

My question is, is there any way to run cron from browser? so that I can test it instantly.

Thanks in advance.

3

4 Answers 4

0

Yes you can test your cron on browser.

Create a model in you module

app/code/Vendor/Module/Model/YourModel.php

add a function

public function doStuff()
{
    //Add cronjob code here
}

Now move all the code of your cron in this function doStuff(). Create frontend contorller

app/code/Vendor/Module/Index/MyController.php

in execute function call your model function

$this->model->doStuff();

where $this->model is the object of your model.
now you can test your code through controller.

After your work is done, call the same model function in your cron

That should work :)

0
7

You may follow the steps below to test a cron job from browser.

Step 1)

Create a PHP file names "cronLaunch.php" under your project root directory

FIle: cronLaunch.php

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

//require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['job'])) {
    define('CRONJOBCLASS', $_GET['job']);
} elseif (php_sapi_name() !== 'cli') {
    die('Please add the class of the cron job you want to execute as a job parameter (?job=Vendor\Module\Class)');
} elseif (!isset($argv[1])) {
    die('Please add the class of the cron job you want to execute enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
    define('CRONJOBCLASS', $argv[1]);
}

class CronRunner 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('adminhtml');
    }

    function launch()
    {
        $cron = \Magento\Framework\App\ObjectManager::getInstance()
            ->create(CRONJOBCLASS);

        $cron->execute();
        return $this->_response;
    }
}

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('CronRunner');
$bootstrap->run($app);

step 2)

Open browser and text the cron job

I assume you your cron class file is MyVendor/MyModule/Cron/CronClass.php

so enter the following URL in your browser and test your cron file.

http://localhost/YOURMAGENTOPROJECT/cronLaunch.php?job=MyVendor\MyModule\Cron\CronClass
3

I recommend you to use n98-magerun2 https://github.com/netz98/n98-magerun2 . After you give the n98-magerun2.phar execute permission you could run

Get all available cron job list.

./n98-magerun2.phar sys:cron:list

To run you specific cron

./n98-magerun2.phar sys:cron:run your_cron_job_code

3
  • I use it all the time, very easy and quick, better than browser
    – medmek
    Commented Jun 11, 2020 at 2:28
  • Good answer. Just installed it, ran my cron job and found the error, all in a few minutes.
    – jdhildeb
    Commented Sep 24, 2020 at 20:31
  • this is a good answer and very appropriate solution for runnign the cron jobs locally to test and develop Commented Dec 6, 2021 at 13:31
0

I have done some modification previously example for choice method in class

<?php

require __DIR__ . '/app/bootstrap.php';

//require '../../app/bootstrap.php';

if (php_sapi_name() !== 'cli' && isset($_GET['job'])) {
    define('CRONJOBCLASS', $_GET['job']);
} elseif (php_sapi_name() !== 'cli') {
    die('Please add the class of the cron job you want to execute as a job parameter (?job=Vendor\Module\Class)');
} elseif (!isset($argv[1])) {
    die('Please add the class of the cron job you want to execute enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
    define('CRONJOBCLASS', $argv[1]);
}

$method = (isset($_GET['method']))? $_GET['method'] : 'execute';
define('CRONJOBMETHOD', $method);

class CronRunner 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('adminhtml');
    }

    function launch()
    {
        $cron = \Magento\Framework\App\ObjectManager::getInstance()
            ->create(CRONJOBCLASS);

        $cron->{CRONJOBMETHOD}();
        return $this->_response;
    }
}

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('CronRunner');
$bootstrap->run($app);

and URL like that

http://your-host/cronLaunch.php?job=MyVendor\MyModule\Cron\CronClass&method=YourMethod

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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