Skip to content

Instantly share code, notes, and snippets.

@atishgoswami
Last active July 10, 2020 09:05
Show Gist options
  • Save atishgoswami/3a0c51cc4892c19f40e8daf043bec3dd to your computer and use it in GitHub Desktop.
Save atishgoswami/3a0c51cc4892c19f40e8daf043bec3dd to your computer and use it in GitHub Desktop.
Adding a custom job using cron:install

To install/register magento cron jobs to crontab of our envirnoments we normally use php bin/magento cron:install, which ends up registering your cronjobs the system crontab

crontab -l

/var/www/html/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/var/log/magento.cron.log
/var/www/html/update/cron.php >> /var/www/html/var/log/update.cron.log
/var/www/html/bin/magento setup:cron:run >> /var/www/html/var/log/setup.cron.log
<type name="Magento\Framework\Crontab\TasksProviderInterface">
    <arguments>
        <argument name="tasks" xsi:type="array">
            <item name="cronMagento" xsi:type="array">
                <item name="command" xsi:type="string">{magentoRoot}bin/magento cron:run | grep -v "Ran jobs by schedule" >> {magentoLog}magento.cron.log</item>
            </item>
            <item name="cronUpdate" xsi:type="array">
                <item name="command" xsi:type="string">{magentoRoot}update/cron.php >> {magentoLog}update.cron.log</item>
            </item>
            <item name="cronSetup" xsi:type="array">
                <item name="command" xsi:type="string">{magentoRoot}bin/magento setup:cron:run >> {magentoLog}setup.cron.log</item>
            </item>
        </argument>
    </arguments>
</type>

Sometimes you might need to register you own custom console command which needs to be registered, so we end up adding it to the our own custom xml

<type name="Magento\Framework\Crontab\TasksProviderInterface">
    <arguments>
        <argument name="tasks" xsi:type="array">
            <item name="CUSTOM_CRON" xsi:type="array">
                <item name="expression" xsi:type="string">* * * * */15</item>
                <item name="command" xsi:type="string">{magentoRoot}bin/magento custom:console:command</item>
            </item>
        </argument>
    </arguments>
</type>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment