0

I'm working on a custom Cron and I want find the best way to call other commands from the Cronjob code in magento2 (programmatically).

For example: When running cli "php bin/magento cache:clean" and so on or my custom console commands.

I try this code but, it is not work..

namespace AmirHome\Cron;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Application;
...    
$arguments = new ArrayInput(['command' => 'my:custom:command']);
$this->getApplication()->find('my:custom:command')->run($arguments, 
$output);

$arguments = new ArrayInput(['command' => 'cache:flush']);
$this->getApplication()->find('cache:flush')->run($arguments, $output);

Error: Call to undefined method AmirHome\Cron\MyCode::getApplication()

1
  • You need to add your custom console command to OS crontab.

    crontab -u <Magento file system owner username> -e

    For example,

    crontab -u magento_user -e

  • Then add your commands with time interval in the end of the file as save,

    * * * * * <path to php binary> <magento install dir>/bin/magento your_command:your_sub_command

    For example,

    * * * * * php /var/www/html/mysite/bin/magento myproduct:updateprice

    Here, * * * * * is the time interval for every minute.

    • CRON_1MINUTE = '* * * * *';
    • CRON_5MINUTES = '*/5 * * * *';
    • CRON_10MINUTES = '*/10 * * * *';
    • CRON_15MINUTES = '*/15 * * * *';
    • CRON_20MINUTES = '*/20 * * * *';
    • CRON_HALFHOURLY = '*/30 * * * *';
    • CRON_HOURLY = '0 * * * *';
    • CRON_2HOURLY = '0 */2 * * *';
    • CRON_DAILY = '0 0 * * *';
    • CRON_TWICEDAILY = '0 0,12 * * *';

For references,

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.