Magento 2 : Execute Cron

by Krupali   Last Updated August 15, 2018 05:09 AM

How can I execute Cron from command line manually.

In Magento 1.x we can run cron like :

www.testsite.com/cron.php 

but in magento 2 how can I do this ?

Please also help me for how can I execute cron from cmd. I already used below command which is not working :

sudo php bin/magento cron:run [--group="customgroupname_cron"]

This is returning exception :

[RuntimeException]   
Too many arguments.  

cron:run [--group="..."] [--bootstrap="..."]

------- Update -------

crontab.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">

    <group id="customgroupname_cron">
        <job name="customgroupname_cron" instance="Namespace\Modulename\Cron\Customcronjob" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

In execute method of above file I put log. But after 1 minute also that is not generated.So how can I came to know that my method is executed.

Tags : magento2 cron cli


Answers 3


You don't need the brackets when you run the command so you should run:

sudo php bin/magento cron:run --group="customgroupname_cron"
Raphael at Digital Pianism
Raphael at Digital Pianism
June 21, 2016 13:24 PM

cron:run [--group="..."] [--bootstrap="..."]

The [] brackets in a command line prototype merely indicate that the arguments they contain are optional.
In this case, it also states that they are chainable.

Julien Lachal
Julien Lachal
June 21, 2016 13:34 PM

I merged the other answers from this post a bit - so that only one file is needed, and cron jobs can be run via browser or command line.

Usage via command line:

php cronLaunch.php "Vendor\Module\Class"

Usage via browser:

https://my.domain/hidden/cronLaunch.php?Vendor\Module\Class

Installation

I suggest to copy the source code from below and to store it in src/pub/hidden/cronLaunch.php. It is very important to protect the hidden directory from any unauthorized access!

<?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);

Thanks and credits to all the other people who posted answers here!

dan
dan
August 15, 2018 04:32 AM

Related Questions


Session is not working in cron file

Updated January 09, 2018 13:09 PM


Cron job permission issue?

Updated January 28, 2018 17:09 PM

How can i setup CRON JOB In wamp Windows?

Updated April 18, 2018 07:09 AM

Start cron job by click button in admin - MAGENTO 2

Updated August 05, 2018 11:09 AM