0

I have a Magento Cron which works fine when I call it using the CLI. I want to call the same Cron in a Controller. Is it possible to do it? (ie) I want to run the execute() function in the Cron script via the Controller code.

2
  • Do you just want to test how your cron works by calling it from the controller or anything else? Commented Sep 13, 2022 at 8:32
  • @SmitaKagwade I actually want to make it such that on click of a button, the cron should execute. Hence I want the button to make a call to a controller, which can then in turn call the cron. Commented Sep 13, 2022 at 8:39

1 Answer 1

1

suppose your cron file is in app/code/Vendor/Module/Cron/RemoveProcut.php, then you can call the cron method in the controller like this :

class YourController extend anyClass{

 protected $cronRemoveProduct;

    /**
     * Constructor
     *
     * @param \Vendor\Module\Cron\RemoveProcut $cronRemoveProduct
     */
    public function __construct(
      \Vendor\Module\Cron\RemoveProcut $cronRemoveProduct
    ){
        $this->cronRemoveProduct = $cronRemoveProduct;
    }
    
    public function execute()
    {
        $this->cronRemoveProduct->execute();
        // another logic
    }
}

hope this help

4
  • Yep this should work. Thanks! Commented Sep 18, 2022 at 19:31
  • For debugging purposes, it will be going well. Commented Dec 23, 2022 at 5:27
  • @LennoxSherwin you're welcome :)
    – Deki
    Commented Dec 25, 2022 at 6:43
  • @DekiAkbar, suppose if we are redirecting to some page in the controller ex admin custom save controller, the page will show as loading till the cron execute function completed. is there any way to avoid this?
    – Manjunath
    Commented Jan 27, 2023 at 6:05

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.