How To

How To Get Last Executed Cron List Programmatically in Magento 2

Hello Magento Friends 👋,

The week begins and so programming! Today let us know How To Get Last Executed Cron List Programmatically in Magento 2. Before beginning, take a peek at the previously released article, How to cancel order Programmatically without disabling URL security key in Magento 2?.

Introduction:

Cron is one of the most astonishing features of Magento 2. Whenever there is any need to schedule tasks, at that time cron is used. Cron helps Magento store owners to execute commands at the given date and time. Cron jobs allow scheduling tasks, reindexing, generating emails, generating newsletters, and much more.

With regards to development reasons, or for analysis or for record purposes, there is a need to figure out the last executed cron list in Magento 2. Let us know How To Get Last Executed Cron List Programmatically in Magento 2 🚀

Steps To Get Last Executed Cron List Programmatically in Magento 2:

Create the Block file at the following path:

app\code\Vendor\Extension\Block\cronlist.php

Now, add the below code.

<?php
namespace Vendor\Extension\Block;

class Info extends \Magento\Config\Block\System\Config\Form\Fieldset
{
    public function __construct(
        \Magento\Backend\Block\Context $context,
        \Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory $cronFactory,
        array $data = [])
    {
        parent::__construct($context, $data);
        $this->cronFactory = $cronFactory;
    }
    /**
    *
     * @return string
     */    private function getCronList()
    {
        $crontabCollection = $this->cronFactory->create();
        $crontabCollection->setOrder('schedule_id')->setPageSize(10);
        $value = '';
        if ($crontabCollection->count() > 0)
        {

            foreach ($crontabCollection as $crontabRow)
            {
                $value .=
                    '<div>'.
                    '<span>' . $crontabRow['status'] . '</span>' .
                    '<span>' . $crontabRow['job_code'] . '</span>' .
                    '<span>' . $crontabRow['created_at'] . '</span>' .
                    '</div>';
            }
        }
        return $value;
    }
}

That’s it!

Conclusion:

So, using the above code you can get the last 10 executed cron list in Magento 2. However, you can change the count as per your requirement.

You may also like to read How to Programmatically Check Particular Cron is Currently Running or not in Magento 2.

In case you face any difficulties while executing the above code, alert me by mentioning your issue in the comment part. Did you found the article useful? Hit the 5 stars and share it with your Magento friends. See you next time till then stay in the know!

Happy Coding 😊

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago