14

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.

1
  • It looks, to me a least, like you can still run them like Mage 1 by doing yoursite.com/update/cron.php
    – tim.baker
    Commented Sep 7, 2016 at 12:28

4 Answers 4

23

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

sudo php bin/magento cron:run --group="customgroupname_cron"
2
  • Yes this is giving a response that "Ran jobs by schedule". But please check my updated quetion.
    – Krupali
    Commented Jun 21, 2016 at 13:33
  • 1
    @Krupali You also need to create a cron_groups.xml file in etc folder if you want to use your own special cron id.
    – Noob Doob
    Commented Jul 1, 2021 at 12:40
10

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!

4
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.

2

To create a custom cron in Magento 2, follow these simple steps:

Step 1: Create a Cron Group

First, create a cron group in a file called cron_groups.xml located at /Namespace/Modulename/etc/cron_groups.xml. Here's an example:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
    <group id="customgroupname_cron">
        <schedule_generate_every>1</schedule_generate_every>
        <schedule_ahead_for>4</schedule_ahead_for>
        <schedule_lifetime>2</schedule_lifetime>
        <history_cleanup_every>10</history_cleanup_every>
        <history_success_lifetime>60</history_success_lifetime>
        <history_failure_lifetime>600</history_failure_lifetime>
        <use_separate_process>1</use_separate_process>
    </group>
</config>

Step 2: Assign Group ID to Your Cron

In your crontab.xml file located at /Namespace/Modulename/etc/crontab.xml, assign the group ID you created to your custom cron job. Here's an example:

<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="custom_cron" instance="Namespace\Modulename\Cron\Customcronjob" method="execute">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

Step 3: Flush Cache and Run Cron

After making these configurations, flush the cache and run the cron command for your custom group using these commands:

php bin/magento c:f
php bin/magento cron:run --group="customgroupname_cron"

That's it! Your custom cron should now be up and running.

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.