1

I am developing a new cronjob, since it is in development phase I want to check the output of cronjob after every edits.

Right now I am creating a log file at the end of cron, and I am executing cron through shell:

php bin/magento cron:run --group=my_cron_group

It is very time taken process to execute cron and check in log file. Is there any way to check cron output like in browser or something else that speedup my development.

Any help would be appreciated.

1

Yes there is a way. You may create a model file in your module which will have logic that cron is executing.

In your cron file, you just need to call that function.

Now to see the preview on faster mode, create frontend route and controller and call the same model function. Once your testing is done, you can delete controller file.

File: [Vendor]/[Module]/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="custommodule" frontName="custommodule">
            <module name="[Vendor]_[Module]"/>
        </route>
    </router>
</config>

File: [Vendor]/[Module]/Controller/Crontest/Index.php

<?php

namespace [Vendor]\[Module]\Controller\Crontest;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    protected $model;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \[Vendor]\[Module]\Model\Custom $model
    )
    {
        parent::__construct($context);
        $this->model = $model;
    }

    public function execute()
    {
        $this->model->executeLogic();
    }
}

File: [Vendor]/[Module]/Model/Custom.php

<?php

namespace [Vendor]\[Module]\Model;

class Custom
{
    public function __construct(

    )
    {

    }

    public function executeLogic()
    {
        die('here');
        // your logic here
    }
}

Now you may call URL like http://yourdomain/custommodule/crontest/index

Call the same function in your cron file as well. Both will have same output. Once your testing is done, you may remove controller file.

  • let me try this and come back to you. – Muhammad Hasham Feb 14 '19 at 7:47
  • Thanks mate. Nice criteria to test cronjob through controller – Muhammad Hasham Feb 14 '19 at 11:21
  • You're welcome !! :) – Yash Shah Feb 14 '19 at 11:24

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.